This article mainly introduces the use of Ajax in PHP some common error summary collation of relevant information, the need for friends can refer to the following
PHP as the backend, the front-end JS using AJAX technology for mutual information transmission, often error, for the novice is a bit unprepared. Summarize errors, experiences, and review them at any time.
The first problem, the current side without errors in the case, page debugging also shows no problem, but Ajax can not get to the back-end PHP file sent over the message:
The front-end code is as follows:
$.ajax ({ URL: ' 1.php ',//destination PHP file data:{"age": "Name": ' ZH '},//transmitted data type: ' Post ',//mode Post/get DataType: ' json ',//data transfer Format success:function (response) { console.log (response); }, error: function (response) { console.log (response); Console.log ("Error");} );
The PHP backend code is as follows:
$postAge = $_post[' age '); $postName = $_post[' name '];echo $postAge; Echo $postName;
After the page appears, F12 debugging looks like this:
Status code is no problem, status is 200,responseready is 4, indicating that the HTML sent to PHP file information process is not a problem. and PHP also returned the information. But why did the program go wrong and not go success?
Be careful at this point! Because multiple echoes in the PHP backend did not organize the data into JSON format. This means that PHP returns a string that is not in JSON format. Some people say add json_encode ()? This is also not possible, because the function of Json_encode () is not clear, Baidu carefully look. Json_encode () is a pair with Json_decode ().
Json_encode (JSON) to organize JSON into JSON-formatted data. In the example above, even if the PHP backend code is rewritten as: Echo Json_encode (PostAge), and Echojsonencode (Postname), it is also wrong. because this is only a single postage and postname in the JSON format, but because it is 2 return, is not only 2 response, in the browser debugging page can also see 1 post back 2 response. This results in 2 JSON-formatted data being returned to the front end as data that is no longer in JSON format (which I understand as JSON contamination for ease of understanding). That is, the single data is in JSON format, but multiple JSON format data are "promiscuous" together and not merged together in JSON format, resulting in "pollution." resulting in overall data format confusion can not be recognized, this situation is data processing and transmission is readily available.
The json_decode (json,true/false) function is to organize the JSON into an array or an object (understood as a class). True is to force the swap to (associative) array, false is the default data that will be converted to object form.
Back to the example presented in this article.
Since the data sent back is no longer data in JSON format, it is the datatype problem.
DataType is to tell the browser to check the transmitted data format. If you do not write, the browser will not check the data format, write it must check and must meet the format requirements. In this case, because it was written in JSON format, but it was not JSON format, so the browser thought that there was an error in the transmission process, so went the error and did not go success.
The best way to do this is to modify the PHP code, change the echo to an array, and use the array's letter form to organize the whole data into JSON format for delivery (Json_encode) to avoid errors.
Of course, you can use another method, similar to cheating method, directly comment out (or do not write) DataType, so that the browser will not check the form of data, but based on the data of the form of intelligent judgment, similar to muddle through.
The following is an explanation of datatype's w3school:
It is important to note that after the multiple echo outputs in the back-end PHP file, the data return is returned, which is correct, and the front end data is 2 data in the form of a string. The data obtained in this example is 12zh.
Of course there are a lot of details, such as the PHP backend can only use echo or Die (), can not use return, this is because return is only on the server side to return data use, and Echo is to print the data, the data from the server to print out, to the front-end. Return can only be returned on the server side, or on a single front end. The strength of Die () is not mentioned, directly terminate the back-end PHP program form return data.
For example, in $,ajax ({}), where each row is a parameter, the arguments are separated by commas, and multiple data are within {}, separated by commas, and so on.
Thank you for reading, hope to help everyone, thank you for the support of this site!
The above is the whole content of this article, I hope that everyone's study has helped.