我使用这个 jquery ajax 代码来传递 JSON:
var jsonObjects = [{id:1, name:"amit"}];
$.ajax({
type: 'GET',
url: 'http://localhost:8080',
data: {
jsonData: JSON.stringify(jsonObjects)
},
dataType: 'json',
complete: function(validationResponse) {
}
});
我已经使用这个节点 js 代码来解析 JSON 数据:
http.createServer(function (request, response) {
response.writeHeader(200, {
"Content-Type": "text/plain"
});
response.writeHead(200, {"Content-Type":"text/plain"});
var theUrl = url.parse(request.url);
var queryObj = queryString.parse( theUrl.query );
var obj = JSON.parse( queryObj.jsonData);
console.log(obj[0].id)
response.write(String(obj[0].id))
response.end();
}).listen(8080,'127.0.0.1');
但它在控制台中显示以下错误:
undefined:1
rn┴P║Eee
^
SyntaxError: Unexpected token u
at Object.parse (native)
at Server.<anonymous> (C:\node\nodejs\node_modules\17-8-12\tracker.js:79:22)
at Server.emit (events.js:70:17)
at HTTPParser.onIncoming (http.js:1610:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:91:29)
at Socket.ondata (http.js:1506:22)
at TCP.onread (net.js:374:27)
然后代码停止..