Some common mistakes in using Ajax in PHP

Source: Internet
Author: User

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‘,//目的php文件   data:{"age":12,"name":‘zh‘},//传送的数据   type:‘post’,//方式post/get   dataType:‘json‘,//数据传送格式   success:function(response)   {   console.log(response);   },   error:function(response)   {   console.log(response);   console.log("错误");   }});

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) , put JSON is organized into JSON-formatted data. In the example above, even if the PHP back-end code is rewritten as: Echo Json_encode ( PosTAg e);and theeChoJso n e NCod e( Postname); it's also wrong.Because it's just a single PosTAg eand the Postname is organized in JSON format, but since it is 2 returns, both 2 response, 1 post back 2 response can be seen on the browser debug page. 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.
Json_decode ( JsoN,TRue/F aLse)Letternumberis aput JSON collation is an array or 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.

 以下是dataType的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.

Some common mistakes in using Ajax in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.