在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中穿。
- 如何通过假设一个默认的内容typesparsingExpress / NodeJs中的缺less内容types的HTTP请求?
- 如何基于caching控制caching对node.js中传出请求的入站响应?
- 如何用express-http-proxy修改响应头
- 为什么express-js不能设置Content-Type头?
- 无法使用gzippo设置Cache-Control标题
- 有条件的服务基于“接受”标题的网页与快递?
- node.js中的头文件 – 套接口io资源解释为脚本,但是以MIMEtypes传递text / plain:
- 在浏览器中查看PDF – 使用POST在Chrome中破解
- 有没有办法来validation一个Ajax请求是从一个给定的域名?