PHP when using ajax summarize some of the common mistakes finishing

This article describes the use of PHP ajax when some of the common mistakes finishing summarize relevant information, you can refer a friend in need

PHP as the back-end, front-end js when using ajax technology transfer for mutual information, often a mistake, for the novice awkward. Summary error, experience, at any time after the review. 

The first question, under the current circumstances end error-free, debugging page also shows no problem, but can not get information back-end ajax php file sent by: 

Front-end code as follows:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$.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( "错误" );
   }
});

php rear end code is as follows:

?
1
2
3
4
$postAge = $_POST [ 'age' ];
$postName = $_POST [ 'name' ];
echo $postAge ;
echo $postName ;

After the page appears, F12 debug view is as follows: 

No problem status code, status is 200, responseReady 4 is described to a process php html file information is not a problem. Php and also returned information. But why the program did not take away the error and success of it? 

At this point we need to be careful! Due to multiple back-end php echo no data organized into json format. That php returns a data string is not json format. Some say plus json_encode () do? This is not enough, because json_encode () function of the role of Mogao Qing, Baidu look carefully. json_encode () and json_decode () are a pair. 

json_encode (json), the data is organized into json json format. In the above example, even if the back-end code php rewritten as: echo json_encode (postAge); and echojsonencode (postName); also wrong. Because it is the only single postAge and postName finishing order json format, but because it is two return, both 2 response, debugging page in a browser can also see a post back 2 response. This results in two data formats json is no longer returned to the front end of a data format json (I understand json pollution, ease of understanding). I.e. a single data format is json json format data but a plurality of "random" together do not merge together in json format will have a "pollution." Resulting in an overall data format confusion can not be identified, get ready to see all data processing and transmission of this case were. 

json_decode (json, true / false) the function is organized into an array or json object (understood as a category). true force is installed for the (associated) arrays, false default is converted to a data form object. 

Back to the examples set forth herein. 

Since the data is not sent back data json format, then it is a question of dataType. 

dataType tells the browser to check the data transfer format. If you do not write, the browser will not go to check the data format, it must write a check and must meet the format requirements. In this example, because the writing is json format, but not the footage back when json format, so the browser thinks occur during transmission error, so the error go away without success. 

In this case the best way is to modify the code php, the echo content to an array, with an array of channels to transmit the entire data arrangement (json_encode) as json format, to avoid errors. 

Of course, you can use another method, similar to the cheating method, comment out directly (or not written) dataType, so the browser does this by examining the form of data but according to the data in the form of intelligent judgment, similar to muddle through.

 The following explanation is W3school dataType of:

Write pictures described here

 

Notably, a plurality of rear end echo php file output, the data is returned with the return indeed, only after the correct modification, the front end of the data obtained two data are combined to form a data string. Data obtained in the present example is 12zh. 

Of course, there are many details of the problem, such as php backend can only echo or die (), can not return, because the return is only returns data for use on the server side, and echo print data, the data from the server print out to the front. return return only a single server, or the distal end. The die () not to mention a powerful, back-end php form of direct termination of the program to return data. 

As well as in $, Ajax ({}); each row only one parameter, the parameters are separated by commas between the plurality of the data in {}, is separated by a comma and the like.

 

Original: https://www.jb51.net/article/106818.htm

Guess you like

Origin www.cnblogs.com/showcase/p/11036641.html