【发布时间】:2015-10-06 07:48:00
【问题描述】:
我目前的“将对象恢复为 JSON 字符串”Express.js 函数是:
router.post('/restore', json_parser, function(request, response, next)
{
console.log('router.post /load');
console.log('Body: ' + request.body);
console.log('Query: ' + request.query);
console.log('href: ' + sanitize(request.user.href));
console.log('identifier: ' + sanitize(request.query.identifier));
var result = look_up_identifier(request.user.href, request.query.identifier);
console.log(result);
response.type('application/json');
response.send(result);
});
客户端的 Ajax save() 函数,目前还没有实现,是:
var save = function(identifier, data)
{
if (Modernizr.localstorage)
{
localStorage[identifier] = JSON.stringify(data);
}
jQuery.ajax('/save',
{
'data':
{
'data': JSON.stringify(data),
'identifier': identifier,
'userid': userid
},
'method': 'POST'
});
}
服务器提供 500 页,我的客户端 restore() 函数(可应要求提供)正在响应给定的非 JSON 输入(在这种情况下,jqxhr.responseText 以“
router.post /load
Body: [object Object]
Query: [object Object]
href: httpsapi.stormpath.comv1accounts[identifier deleted]
POST /restore 500 7.343 ms - 1202
router.post /load
Body: [object Object]
Query: [object Object]
href: httpsapi.stormpath.comv1accounts[identifier deleted]
POST /restore 500 3.410 ms - 1202
router.post /load
Body: [object Object]
Query: [object Object]
href: httpsapi.stormpath.comv1accounts[identifier deleted]
而且localStorage目录是空的。
我希望能够通过 Ajax 发送值,无论是通过 POST 正文中的 QUERY_STRING 还是 JSON,并在服务器端挑选出我在客户端编码的任何内容。出了点问题,我还没有得到一本字典,我可以在其中从客户端探测其中一个键,并从我通过 QUERY_STRING 或 JSON 编码的内容中获取常规值。
我需要以不同的方式完成什么?
【问题讨论】:
-
你安装了bodyparser中间件吗?什么是 json_parser?
-
您的 console.logs 可以更改为更有帮助。不要将字符串与对象连接,而是使用逗号。
console.log('Body: ', request.body)现在您实际上可以看到数据(或缺少数据)
标签: jquery ajax json node.js express