第一种方式:
前端
$.ajaxSetup({
contentType: "application/json; charset=utf-8"
});
$.post('/test', JSON.stringify({
"Email": "sfpe@163.com",
"Password": "123456",
"person": {
"age": 25
}
}), function(data){
debugger
});
后端
console.log(util.inspect({a: req.body}));
console.log(util.inspect({a: req.body.Email}));
结果
{ a:
{ Email: 'sfpe@163.com',
Password: '123456',
person: { age: 25 } } }
{ a: 'sfpe@163.com' }
第二种方式
前端
$.post('/test', {
post:JSON.stringify({
"Email": "sfpe@163.com",
"Password": "123456",
"person": {
"age": 25
}
})
}, function(data){
debugger
});
后端
console.log(util.inspect({a: req.body.post}));
console.log(util.inspect({a: req.body.post.Email}));
结果
{ a: '{"Email":"sfpe@163.com","Password":"123456","person": {"age":25}}' }
{ a: undefined }
问题:哪种传给后端的是json格式 ? 我用第二种传了,后端说不是json的格式....用第一种,报了这种错误: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
我现在都不知道是我的问题 还是后端的问题。
跨域请求处理