How to use Ajax to read data in JSON _ajax related

Source: Internet
Author: User
Tags eval

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.

Related Article

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.