Summarize some common errors and ajax common errors when using ajax in PHP
When PHP is used as the backend, errors often occur when front-end js uses ajax technology for mutual information transfer, which is a bit difficult for beginners. Summarize errors and experiences, and review them at any time.
First, when there is no error at the front end, page debugging also shows no problem, but ajax cannot get the information sent from the back-end PHP file:
The front-end code is as follows:
$. Ajax ({url: '1. php ', // target php file data: {"age": 12, "name": 'zh'}, // type of the transmitted data: 'post ', // method post/get dataType: 'json', // Data Transmission 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, the F12 debugging view is as follows:
The status code is okay. status is 200 and responseReady is 4, which means there is no problem in the process of sending information to the PHP file in html. Php also returns information. But why did the program go through error instead of success?
Be careful! Because the php backend does not organize data into json format for multiple echo. That is to say, php returns a string that is not in json format. Some people say that json_encode () is added? This is also not acceptable because the functions of json_encode () are not clearly understood. Baidu will take a closer look. Json_encode () and json_decode () are a pair.
Json_encode (json), which sorts json data into json format. In the above example, even if the php backend code is rewritten to echo json_encode (postAge); and echojsonencode (postName);, this is also incorrect.In this way, only a single postAge and postName are organized in json format. However, because there are two responses, both two response are returned. On the browser debugging page, you can also see that one post returns two response. As a result, two data in json format are returned to the front-end, which is no longer in json format (I think it is json-contaminated and easy to understand ). That is, if a single data is in json format, but multiple data in json format are "randomly" combined and not merged in json format, it will produce "pollution ". As a result, the overall data format is messy and cannot be identified. In this case, the data processing and transmission can be seen at any time.
The json_decode (json, true/false) function sorts json into arrays or objects (understood as classes ). "True" indicates mandatory replacement for (associated) arrays. "false" indicates that data in the object format is converted by default.
Return to the example in this article.
Since the transmitted data is no longer in json format, it is a problem with dataType.
DataType tells the browser to check the transmitted data format. If no data is written, the browser does not check the data format. If the data is written, the browser must check the data format and meet the format requirements. In this example, because the data is written in json format, but the returned data is not in json format, the browser considers that an error occurs during transmission, so the error is taken without success.
In this case, the best way is to modify the php code, change the echo content to an array, and organize the overall data into json format (json_encode) in the form of an array to avoid errors.
Of course, you can also use another method, similar to cheating, to directly comment out (or not write) dataType, in this way, the browser will not check the form of data, but intelligent judgment based on the form of data, similar to passing through the blind.
The following is the description of the W3school of dataType:
It is worth noting that after multiple echo outputs in the back-end PHP file, the data returned is indeed returned together. After the modification is correct, the data obtained by the front-end is two data in the form of a string. The data obtained in this example is 12zh.
Of course there are still many details. For example, the php backend can only use echo or die (), but cannot use return, because return is only used to return data on the server side, echo prints data from the server to the front end. Return can only be returned on the server side or a single front-end. The strength of die () is not mentioned, and data is directly returned in the form of a backend php program.
For example, in $, ajax ({}), each row is a parameter, and parameters are separated by commas (,). Multiple Data is separated by commas.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!