gpt4 book ai didi

javascript - 如何通过 URL 传递撇号?

转载 作者:IT老高 更新时间:2023-10-28 22:05:36 25 4
gpt4 key购买 nike

我正在使用 Node.js :

var s = 'Who\'s that girl?';
var url = 'http://graph.facebook.com/?text=' + encodeURIComponent(s);

request(url, POST, ...)

这不起作用! Facebook 切断了我的文字...

完整代码:

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 在墙上贴了一个空白帖子。没有文字显示。什么都没有。

当我记录我的 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

很明显,如果您查看该 URL,您会发现撇号的编码不正确。

最佳答案

有同样的问题,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

这样,trackArtistTitle 将在服务器上正确解码,即使用 PHP 使用 urldecode()。

关于javascript - 如何通过 URL 传递撇号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7298770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com