简体   繁体   English

无法识别AJAX发布数据

[英]AJAX post data is not recognized

I have a made an AJAX request. 我提出了AJAX请求。

Here is its setting: 这是它的设置:

    $.ajax({

        data : { id : 25 },
        dataType : 'json',
        contentType : 'application/json; charset=utf-8',
type  : 'POST',
        // the rest of the setting
        });

And here is the server side: 这是服务器端:

header("contentType=application/json");

// and then the rest of the request

Everything works. 一切正常。 Data is returned through JSON and no problem. 数据通过JSON返回,没有问题。 But the $_POST is not filled with any data, though when I check the ajax request log through firebug I see that it sends the id...what could be the problem? 但是$ _POST没有填充任何数据,尽管当我通过firebug检查ajax请求日志时,我看到它发送了id ...可能是什么问题? The problem exists when I set the contentType header and dataType... 当我设置contentType标头和dataType时存在问题。

I also have set Allow Origin header but the problem is not solved... 我还设置了Allow Origin标头,但问题没有解决...

I also have checked the data with $_POST and $_REQUEST 我还用$_POST$_REQUEST检查了数据

I use LARAVEL framework... 我使用LARAVEL框架...

The default method type for jQuery's ajax method is GET . jQuery的ajax方法的默认方法类型是GET Try to set the method type to POST in your settings, like this: 尝试在您的设置中将方法类型设置为POST ,如下所示:

$.ajax({
    type: 'POST',               // <<<<
    data : { id : 25 },
    dataType : 'json',
    contentType : 'application/json; charset=utf-8',
    // the rest of the setting
});

For more information about ajax method, please refer here . 有关ajax方法的更多信息,请参考此处

Update 更新资料

I think it would be better not to specify the contentType . 我认为最好不要指定contentType The official documentation says: 官方文件说:

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8') contentType(预设值:'application / x-www-form-urlencoded; charset = UTF-8')

When sending data to the server, use this content type . 将数据发送到服务器时,请使用此内容类型 Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases . 默认值为“ application / x-www-form-urlencoded; charset = UTF-8”, 在大多数情况下都可以 If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). 如果您将内容类型显式传递给$ .ajax(),则该内容类型将始终发送到服务器(即使未发送任何数据)。 The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; W3C XMLHttpRequest规范规定该字符集始终为UTF-8。 specifying another charset will not force the browser to change the encoding. 指定其他字符集不会强制浏览器更改编码。

you should check with $_GET to see if the data is there. 您应该使用$ _GET查看数据是否存在。 if you are not explicitly defining any method by which to send the data, then by default the GET method is used. 如果您没有显式定义发送数据的任何方法,那么默认情况下将使用GET方法。 either set the method as POST or retrieve the data through $_GET to see if it is there. 将方法设置为POST或通过$ _GET检索数据以查看是否存在。

add type in ajax 在ajax中添加类型

$.ajax({

data : { id : 25 },
dataType : 'json',
type :'POST'
contentType : 'application/json; charset=utf-8',
// the rest of the setting

})

您没有传递类型,因此默认情况下它是一个“ 获取请求”,签入$_GET[]或在ajax调用中添加类型POST

you miss to declare type POST use as following way 您错过了声明类型POST使用的方式,如下所示

$.ajax({

data : { id : 25 },
dataType : 'json',
type :'POST',
contentType : 'application/json; charset=utf-8',
// the rest of the setting

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM