How to Use ajax to read Json data and ajax to read json data
This article will share with you how to use ajax to read data in Json.
I. Basic Knowledge
What is json?
JSON refers to the JavaScript Object Notation (JavaScript Object Notation)
JSON is a lightweight text data exchange format.
JSON is independent of the Language *
JSON is self-descriptive and easy to understand.
JSON uses JavaScript syntax to describe data objects, but JSON is still independent of the language and platform. The JSON parser and JSON library support many different programming languages.
JSON-convert to JavaScript Object
The JSON text format is syntactically the same as the code used to create JavaScript objects.
Because of this similarity, there is no need for a parser. JavaScript programs can use built-in eval () functions to generate native JavaScript objects using JSON data.
Ii. Reading data in Json
First, I wrote a Json file containing the content. Note the format.
Compile a json File
Then, write the html code and reference ajax.
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
The encapsulated AJAX Function Code is as follows:
/* AJAX encapsulation function url: the address of the file to be read by the system fnSucc: A function. After the file is retrieved, it will call */function ajax (url, fnSucc, fnFaild) {// 1. create an Ajax object var oAjax = null; if (window. XMLHttpRequest) {oAjax = new XMLHttpRequest ();} else {oAjax = new ActiveXObject ("Microsoft. XMLHTTP ");} // 2. connect to the server oAjax. open ('get', url, true); // 3. send a request to oAjax. send (); // 4. receives the oAjax response from the server. onreadystatechange = function () {if (oAjax. readyState = 4) // complete {if (oAjax. status = 200) // {fnSucc (oAjax. responseText);} else {if (fnFaild) fnFaild (oAjax. status );}}};}
The next step is to read the file content. Before that, it is worth mentioning that AJAX reads the file from the server, so we need to put the written JSON file under the server path, A beginner may have been exposed to IIS only. His file path is C: \ inetpub \ wwwroot \ aspnet_client \ system_web. Just put the Json file in this path and use localhost to access the server, you can.
Read
The above is all the content of this article, hoping to help you learn.