This article is to share with you how to use Ajax to read data in JSON.
First, basic knowledge
What is JSON?
JSON refers to the JavaScript Object notation (JavaScript objects notation)
JSON is a lightweight text data interchange Format
JSON is independent of language *
JSON is self-describing and easier to understand
JSON uses JAVASCRIPT syntax to describe data objects, but JSON is still independent of language and platform. The JSON parser and the JSON library support many different programming languages.
JSON-Convert to JavaScript object
The JSON text format is syntactically the same as the code that creates the JavaScript object.
Because of this similarity, JavaScript programs can use the built-in eval () function to generate native JavaScript objects with JSON data without a parser.
Second, read data in the JSON
First I wrote a JSON file with content in it. Note the format.
Figure a file to write JSON
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 wrapper function
URL: The system to read the address of the file
fnsucc: A function, file fetch, after loading will call/
function ajax (URL, fnsucc, fnfaild)
{
//1. Create Ajax object
var oajax=null;
if (window. XMLHttpRequest)
{
oajax=new xmlhttprequest ();
}
else
{
oajax=new activexobject ("Microsoft.XMLHTTP");
}
2. Connect Server
oajax.open (' Get ', url, true);
3. Send request
oajax.send ();
4. Return of the receiving server
oajax.onreadystatechange=function ()
{
if (oajax.readystate==4)//complete
{
if ( OAJAX.STATUS==200) //Success
{
fnsucc (oajax.responsetext);
}
else
{
if (fnfaild)
fnfaild (Oajax.status);}}}
;
}
The next step is to read out the contents of the file, before that, one thing to mention is that Ajax is read from the server file, so to put the written JSON file on the server path, the novice may contact the server only IIS, his file path is C:\inetpub\wwwroot\ Aspnet_client\system_web, just put the JSON in this path, and then use localhost to access the server, you can.
Figure II Reading effect diagram
The above is the entire content of this article, I hope to help you learn.