How to Use ajax to read Json data and ajax to read json data

Source: Internet
Author: User

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.

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.