在node.js中parsing的JSON数据在控制台中显示为undefined

在node.js中parsing的JSON数据在控制台中显示为undefined ..

这里是HTML代码:

var jsonObjects = [{id:1, name:"amit"}]; jQuery.ajax({ url: 'http://localhost:8081', type: "POST", data: {"id":"1", "name":"amit"}, dataType: "json", success: function(result) { alert("done") } }); 

这里是Nodejs代码:

 http.createServer(function (request, response) { response.writeHead(200, {"Content-Type":"text/plain"}); var urlObj = JSON.stringify(response.data, true); console.log(urlObj) response.end(); }).listen(8081); 

尝试使用GET方法而不是Post。 尝试这个

 var jsonObjects = [{id:1, name:"amit"}]; $.ajax({ type: 'GET', url: 'http://localhost:8081', data: { jsonData: JSON.stringify(jsonObjects) }, dataType: 'json', complete: function(validationResponse) { } }); 

数据将保存在请求中,而不是来自客户端请求的响应。

如果你使用快递来创build你的http服务器,你还需要告诉它使用bodyParser中穿。