Node.js 中查询字符串解析被撇号截断
我正在 Node.js 中构建一个服务器,用于从网站上的 Javascript 接收数据。使用 jQuery 的 getJSON 方法发送数据,如下所示:
$.getJSON(
url,
{
id: track.id,
artist: track.artist,
title: track.title,
album: track.album,
filename: track.filename,
user: track.user,
userId: track.userId,
room: track.room,
timestamp: track.timestamp
}
);
然后,我尝试使用 Node 的 url 模块获取数据,如下所示:
var urlObj = url.parse(request.url, true);
var jsonCallback = urlObj.query["callback"];
这在大多数情况下工作正常,但当其中一个参数包含撇号时,它会失败。看起来它正在停止解析撇号处的查询字符串。以下是 console.log 针对两个不同查询对象打印的内容:
{ callback: 'jQuery15105242477038409561_1304925579219',
id: '6c91c74db064c93f1f020000',
artist: 'Radiohead',
title: 'Everything In Its Right Place',
album: 'Kid A',
filename: '01 Everything In Its Right Place.m4a',
user: 'Cowrelish',
userId: '82a89b4df7a9120305000000',
room: 'qwantz',
timestamp: '1304924972555',
_: '1304925611362' }
{ callback: 'jQuery15105242477038409561_1304925579221',
id: '798cc74dfcce4337f7010000',
artist: 'Toy Dolls',
title: 'The Final Countdown',
album: 'HOLY SHIT!!! IT' }
第二个查询对象的专辑是“HOLY SHIT!! IT'S THE FINAL COUNTDOWN!”,正如您所看到的,它在撇号处被截断了。
我尝试在客户端代码中转义撇号,但我得到的只是“album: 'HOLY SHIT!!! IT\\'”。它避开了斜杠,但没有避开撇号。
这是一个错误还是一个功能?我应该在客户端以不同的方式转义撇号,还是修复 Node.js 中的错误?
I'm building a server in Node.js that receives data from Javascript on a website. The data is being sent using jQuery's getJSON method, like so:
$.getJSON(
url,
{
id: track.id,
artist: track.artist,
title: track.title,
album: track.album,
filename: track.filename,
user: track.user,
userId: track.userId,
room: track.room,
timestamp: track.timestamp
}
);
I'm then trying to get at the data using Node's url module like this:
var urlObj = url.parse(request.url, true);
var jsonCallback = urlObj.query["callback"];
This works fine most of the time, but it fails when one of the parameters contains an apostrophe. It looks like it's stopping parsing the query string at the apostrophe. Here's what console.log prints for two different query objects:
{ callback: 'jQuery15105242477038409561_1304925579219',
id: '6c91c74db064c93f1f020000',
artist: 'Radiohead',
title: 'Everything In Its Right Place',
album: 'Kid A',
filename: '01 Everything In Its Right Place.m4a',
user: 'Cowrelish',
userId: '82a89b4df7a9120305000000',
room: 'qwantz',
timestamp: '1304924972555',
_: '1304925611362' }
{ callback: 'jQuery15105242477038409561_1304925579221',
id: '798cc74dfcce4337f7010000',
artist: 'Toy Dolls',
title: 'The Final Countdown',
album: 'HOLY SHIT!!! IT' }
The album for the second one is "HOLY SHIT!! IT'S THE FINAL COUNTDOWN!", as you can see it's been truncated at the apostrophe.
I've tried escaping the apostrophe in the client code, but all I get then is "album: 'HOLY SHIT!!! IT\\'". It's escaping the slash, but not the apostrophe.
Is this a bug or a feature? Should I be escaping apostrophes in a different way on the client, or looking to fix a bug in Node.js?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必在 jquery 端转义,并且它似乎不是节点错误(至少在最新版本中)。
我尝试复制您的问题,但无法使用节点 0.4.7。
我这样做了:
使用刚刚在传入请求上执行此操作的节点代码:
这给了我:
所以,似乎不是 jQuery 或 Node.js 的普遍问题。
您在解析之前查看过完整的网址吗?您应该能够将其复制并粘贴到节点交互式 shell 中,然后执行类似的操作来测试:
浏览器和 Web 服务器之间可能会接触 URL 的任何内容?
You shouldn't have to escape on the jquery side, and it doesn't appear to be a node bug (at least with latest release).
I tried to duplicate your problem and wasn't able to using node 0.4.7.
I did this:
with node code that just did this on the incoming request:
which gave me:
So, doesn't seem to be a general problem with either jQuery or node.
Have you looked at the full url before it is parsed? You should be able to just copy and paste that into the node interactive shell and do something like this to test:
Anything between the browser and the web server that might be touching the url?