我已经做了一个AJAX请求。
下面是它的设置:
$.ajax({
data : { id : 25 },
dataType : 'json',
contentType : 'application/json; charset=utf-8',
type : 'POST',
// the rest of the setting
});
服务器端:
header("contentType=application/json");
// and then the rest of the request
一切正常。数据通过JSON返回,没有问题。但是$_POST没有填充任何数据,尽管当我通过firebug检查ajax请求日志时,我看到它发送id…有什么问题吗?当我设置contentType头和dataType时存在问题…
我也设置了Allow Origin
头,但问题没有解决…
我也检查了$_POST
和$_REQUEST
的数据
jQuery的ajax
方法的默认方法类型是GET
。尝试在设置中将方法类型设置为POST
,如下所示:
$.ajax({
type: 'POST', // <<<<
data : { id : 25 },
dataType : 'json',
contentType : 'application/json; charset=utf-8',
// the rest of the setting
});
有关ajax
方法的更多信息,请参阅此处。
我认为最好不要指定contentType
。官方文件说:
contentType(默认:'application/x-www-form-urlencoded;utf - 8字符集= ')
向服务器发送数据时,使用该内容类型。默认为"application/x-www-form-urlencoded;charset=UTF-8", 在大多数情况下是可以的。如果显式地将内容类型传递给$.ajax(),那么它总是被发送到服务器(即使没有发送数据)。W3C XMLHttpRequest规范规定字符集总是UTF-8;指定另一个字符集不会强制浏览器更改编码。
您应该使用$_GET检查数据是否存在。如果没有显式定义发送数据的任何方法,则默认情况下使用GET方法。要么将方法设置为POST,要么通过$_GET检索数据来查看它是否存在。
添加类型在ajax
$.ajax({
data : { id : 25 },
dataType : 'json',
type :'POST'
contentType : 'application/json; charset=utf-8',
// the rest of the setting
})
你没有传递类型所以它是一个Get请求默认情况下,检查在$_GET[]
或添加类型POST在ajax调用
您没有按照以下方式声明type POST使用
$.ajax({
data : { id : 25 },
dataType : 'json',
type :'POST',
contentType : 'application/json; charset=utf-8',
// the rest of the setting
});