Ajax asynchronous request for JSon data (graphic details), ajaxjson
In the previous article, we talked about the text type of Ajax request data. Both text and html are simple data processing. While in the programming process, when Ajax is used to call data, it is inevitable that logic processing is required, the accepted data is also complicated, for example, the data of the array type. At this time, the JSON data type needs to be used for processing. Today we will talk about some details in the JSON data request process:
The Helper family reminds you of the tools and raw materials required in this article as follows:
Wamp or lamp environment, jquery. js, Editor
For details, see the following:
1. Create the basic file structure json_ajax.html and json_ajax.php. Download jquery. js,
2.compile the code of the json_ajax.html and json_ajax.php files separately:
3.run json_ajax.html and json_ajax.php in the w(lw.ampenvironment. The running result is as follows:
The running result should be normal, and there is no difficulty in understanding it. Continue to look down. If you are confused here, there is no need to continue reading.
4.after analyzing the source code and running result, we know that the Click Event of the button is set in json_ajax.html, and click the button. The normal running result is as follows;
If the display is not normal, refer to my jQuery Ajax debugging.
5. Result Analysis in Step 1:
Console. log (data) directly returns the result returned by json_ajax.php, which is a json string;
Console. log (eval ('[' + data + ']') to convert the json string returned by json_ajax.php to an object.
The 'object' is different from the process in PHP, but in JS, all the data is an object. Maybe now I understand why I want to convert the json string into an object; processing objects in js is more convenient, just like when processing data in PHP, the first thing that comes to mind is array.
6. Summary:
1. php must return json, that is, data must be processed using json_encode;
2. After ajax accepts data, it needs to be processed by eval (). '[' + data + ']' Can Be Used '(' + data + ')';
3. console. log () can be replaced by alert;
4. Here is the process idea. Once data is received, logic processing can be performed as needed.
The preceding illustration demonstrates Ajax asynchronous request for JSon data.