PHP just used a few days, is not my main involved, the previous processing has been forgotten almost, but after resolving the cross-domain, using jquery ajax to find a lot of problems, using Web pages and third-party post test plug-in can not get a consistent response? Personal solutions, although relatively low, but simple to use is OK, of course, there are better practices, I am all ears
In cases where cross-domain issues do not occur, the use of $.ajax is generally able to obtain a response
$.ajax ({url: ' index.php ', data:{"data": {"name": "Fred", "info": {"id": 1, "CID": 1009}}, type: ' POST ', success:function (data) {alert (data); }, error (XHR) {alert (' error\n ' +xhr.responsetext)}
index.php
<?phpheader ("access-control-allow-origin:*"); Header ("Content-type:text/html;charset=utf-8"); Echo $_POST[' Data ']
By default, $.ajax that do not specify Content-type are submitted in application/x-www-form-urlencoded form, and the $_post default for PHP only recognizes this.
However, sending application/x-www-form-urlencoded,Raw-json, and form-data through third-party post test tools (such as postman, etc.) cannot get the correct response (in the wrong format, Need decoding, etc.)
In other words, the browser with the tool is not unified, at this time simply to make a judgment on it
/*** This PHP only makes a POST request */$data = Isset ($GLOBALS ["Http_raw_post_data"])? $GLOBALS ["Http_raw_post_data"]: "", if ($data) {///if $data is not empty, the entity sent is in $globals["Http_raw_post_data"] (that is, it is possible to raw-j Son sent over) $fd =json_decode ($data, true);//decode into an array object//The following sentence is an additional function, can be submitted with "data" key can also be without the "information", the final value for data corresponding However, if the default contenttype is not modified, the $.ajax must be sent with "data" if (Isset ($fd [' data ')) {$fd = $fd [' Data '];}} else{$fd =$_post[' data '];if (!is_array ($FD)) {//If the post is sent over a string, it is decoded into an array object $fd=json_decode ($FD, True);}} $FD is {"Name": "Fred", "info": {"id": 1, "CID": 1009}} is just an array of PHP ("name" = "Fred",...)
Well, the last thing I have to say is a problem:
If the server side returns correctly when using $.ajax and the commit data type datatype is set to JSON, the foreground page gets no response?
In fact, this time the server will return these to $.ajax callback error (XHR), through Xhr.responsetext can get information, this is not quite understand, if the great God know, Inform, tks!
$.ajax sending data does not get the correct response