Javascript 如何通过 URL 传递撇号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7298770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you pass an apostrophe through a URL?
提问by user847495
I'm using Node.js:
我正在使用Node.js:
var s = 'Who\'s that girl?';
var url = 'http://graph.facebook.com/?text=' + encodeURIComponent(s);
request(url, POST, ...)
This does not work! And Facebook cuts off my text...
这不起作用!然后 Facebook 切断了我的文字...
Full code:
完整代码:
function postToFacebook(fbid, access_token, data, next){
var uri = 'https://graph.facebook.com/'+String(fbid)+'/feed?access_token='+access_token;
var uri += '&' + querystring.stringify(data);
request({
'method':'POST',
'uri': uri,
},function(err,response,body){
next();
});
};
app.get('/test',function(req,res){
var d = {
'name':'Who\'s that girl?',
'link': 'http://example.com',
'caption': 'some caption...',
'description': 'some description...',
'picture': 'http://i.imgur.com/CmlrM.png',
};
postToFacebook(req.user.fb.id, req.user.fb.accessToken, d);
res.send('done');
});
Facebook gets a blank post on the wall. No text shows. Nothing.
Facebook 在墙上收到一条空白帖子。没有文字显示。没有。
When I log my URI, it is this:
当我记录我的 URI 时,它是这样的:
https://graph.facebook.com/1290502368/feed?access_token=2067022539347370|d7ae6f314515c918732eab36.1-1230602668|GtOJ-pi3ZBatd41tPvrHb0OIYyk&name=Who's%20that%20girl%3F&link=http%3A%2F%2Fexample.com&caption=some%20caption...&description=some%20description...&picture=http%3A%2F%2Fi.imgur.com%2FCmlrM.png
Obviously if you take a look at that URL, you see that the apostrophe is not being encoded correctly.
显然,如果您查看该 URL,您会发现撇号没有正确编码。
采纳答案by Jamund Ferguson
I'm doing a similar thing (also with Node.js) and first tried using JavaScript's built-in escape()function, but it didn't really work.
我正在做类似的事情(也使用 Node.js)并首先尝试使用 JavaScript 的内置escape()函数,但它并没有真正起作用。
Here's how I ended up getting search to work. It might just be a fluke:
这就是我最终让搜索工作的方式。这可能只是侥幸:
function doMySearch(showTitle) {
showTitle = escapeShowTitle(showTitle)
var url = "http://graph.facebook.com/search?q=" + showTitle + "&type=page"
doSomethingWith(url)
}
function escapeShowTitle(title) {
title = title.replace(/'/g, "")
title = escape(title)
return title
}
doMySearch("America's Funniest home Videos")
回答by nidalpres
Had the same problem, encodeURIComponent didn't encode single quote. The trick is to do the replacement of ' with %27, afterthe encoding:
有同样的问题, encodeURIComponent 没有编码单引号。诀窍是在编码后用 %27 替换 ' :
var trackArtistTitle = encodeURIComponent("Johnny Vegas - Who's Ready Fo'r Ice Cre'am")
// result: Johnny%20Vegas%20-%20Who's%20Ready%20Fo'r%20Ice%20Cre'am
trackArtistTitle = trackArtistTitle.replace(/'/g, '%27')
// result: Johnny%20Vegas%20-%20Who%27s%20Ready%20Fo%27r%20Ice%20Cre%27am
This way, trackArtistTitle will be properly decoded on server i.e. with PHP using urldecode().
这样,trackArtistTitle 将在服务器上正确解码,即使用 urldecode() 的 PHP。
回答by daniel.caspers
I know this doesn't address the OP's question, but for those coming here with OData Queryrelated questions, note that the escape character is yet another single quote.
我知道这并没有解决 OP 的问题,但是对于那些带着OData Query相关问题来到这里的人,请注意转义字符是另一个单引号。
unescapedValue.replace(/'/g, '\'\'')
This assumes you have already performed an encodeURIComponent(unescapedValue)
on your string
这假设您已经encodeURIComponent(unescapedValue)
对字符串执行了 an
回答by Samir Adel
You can encode the single quote as specified in this link http://www.w3schools.com/TAGS/ref_urlencode.asp
您可以按照此链接http://www.w3schools.com/TAGS/ref_urlencode.asp 中指定的方式对单引号进行编码