PHP关于ajax获取参数踩过的坑

Posted 想想想吃鱼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP关于ajax获取参数踩过的坑相关的知识,希望对你有一定的参考价值。

Common Mistake #7: Assuming $_POST will always contain your POST data

Despite its name, the $_POST array won't always contain your POST data and can be easily found empty. To understand this, let's take a look at an example. Assume we make a server request with a jQuery.ajax() call as follows:

// js
$.ajax({    url: 'http://my.site/some/path',    method: 'post',    data: JSON.stringify({a: 'a', b: 'b'}),    contentType: 'application/json'});

(Incidentally, note the contentType: 'application/json' here. We send data as JSON, which is quite popular for APIs. It's the default, for example, for posting in the AngularJS $http service.)

在现在的主流前端框架,ajax请求采用的contentTyle都是‘application/json’

当我尝试用$_POST去获取传递的参数时候,得到如下:

On the server side of our example, we simply dump the $_POST array:

// php

var_dump($_POST);

Surprisingly, the result will be:

array(0) { }

Why? What happened to our JSON string {a: 'a', b: 'b'}?

The answer is that PHP only parses a POST payload automatically when it has a content type of application/x-www-form-urlencoded or multipart/form-data

如果想通过$_POST获的参数,contentType必须是 ‘application/x-www-form-urlencoded’ 或者 multipart/form-data 

此时我们可以通过另一种方式来获取参数。

The reasons for this are historical --- these two content types were essentially the only ones used years ago when PHP's $_POST was implemented. So with any other content type (even those that are quite popular today, like application/json), PHP doesn't automatically load the POST payload.

Since $_POST is a superglobal, if we override it once (preferably early in our script), the modified value (i.e., including the POST payload) will then be referenceable throughout our code. This is important since $_POSTis commonly used by PHP frameworks and almost all custom scripts to extract and transform request data.

So, for example, when processing a POST payload with a content type of application/json, we need to manually parse the request contents (i.e., decode the JSON data) and override the $_POST variable, as follows:

// php$_POST = json_decode(file_get_contents('php://input'), true);

Then when we dump the $_POST array, we see that it correctly includes the POST payload; e.g.:

array(2) { ["a"]=> string(1) "a" ["b"]=> string(1) "b" }



接下来在讲一讲vue的axios使用PUT请求php服务端出现404的情况

let config = {        headers : {            'Content-Type':'application/json;charset=UTF-8'        },    };

axios.put(this.authUrl,JSON.stringify(this.userInfo),config)    .then(res => {        console.log(res)    })    .catch(err => {         console.log(err)    })

上面的代码在请求之后的结果如下

此时看到请求头重显示的 request method是 OPTIONS

后来查询各种资料发现:根源在于,我们发出去的请求不是

 simple request,那么在每次发送请求之前,都会发送一个options请求,simple request 需要同时满足以下条件(规范可以百度查询):

  1. get、post、head 请求类型

  2. 不要设置列表之外的header(如: user-agent)

  3. Content-Type 只能是:

    1. application/x-www-from-urlencoded

    2. multipart/from-data

    3. text/plain

所以我们只需要在收到OPTIONS请求的时候及时响应请求:

并在头部添加允许接受OPTIONS的请求。


以上是关于PHP关于ajax获取参数踩过的坑的主要内容,如果未能解决你的问题,请参考以下文章

技术分享| Linux高并发踩过的坑及性能优化

Android easeui 3.0 即时通讯 我踩过的坑

技术分享| Linux高并发踩过的坑及性能优化

那些年,我们在Django web开发中踩过的坑——神奇的‘/’与ajax+iframe上传

编程中踩过的坑

关于任务管理器踩过的坑