JQuery Post发送表单数据而不是JSON [英] JQuery Post sends form data and not JSON
本文介绍了JQuery Post发送表单数据而不是JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试发送json。这是我的功能:
Trying to send json. Here's my function:
var object = ... ;
$.ajax({
type: 'POST',
url: '<url>',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: object
});
但每当我查看Chrome时,它总是将其作为查询参数发送:
But whenever I check Chrome, it always sends it as query params:
Request Payload:
startDate=Wed+Dec+19+2012+19%3A00%3A00+GMT-0500+(EST)&endDate=Thu+Dec+20+2012+19%3A00%3A00+GMT-0500+(EST)&
如何将其作为JSON发送?
How do I get it to send as JSON?
推荐答案
使用 JSON.stringify(object)
示例:
$.ajax({
type: 'POST',
url: '<url>',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(object)
});
注意并非所有浏览器都支持JSON.stringify( http://caniuse.com/#feat=json ),特别是IE7及更低版本的浏览器。
Note JSON.stringify is not supported in all browsers (http://caniuse.com/#feat=json ), in particular browsers IE7 and lower.
如果你也需要支持这个浏览器,你可以使用这个Javascript库: https://github.com/douglascrockford/JSON-js
If you need to support this browsers too you can use this Javascript library: https://github.com/douglascrockford/JSON-js
这篇关于JQuery Post发送表单数据而不是JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文