Native Ajax receives JSON string (simple Introduction)

Source: Internet
Author: User

What is JSON? JSON is full of JavaScript object Notation (JavaScript Object notation), which is based on the literal of JavaScript objects, and if you look at it from the eyes, the data in the JSON is stored in curly braces-{}, If further analysis is used, JSON is a data interchange format. The JSON interior is arranged in the form of a name: value, such as the following Json1, which is a JSON object.
var json2={"name": "Zhang San", "age": +, "Sex": "Boy"}
What is a JSON string? In the literal sense, add double quotation marks (or single quotation marks) around the JSON, and to avoid the internal double quotes, we put the single quotation mark outside, which is a JSON string, as in the following Json2
var json2= ' {' name ': ' Zhang San ', ' age ': +, ' sex ': ' Boy '} '
Method One: Use the Eval method to parse a JSON string into a JSON objectThere are no compatibility issues with EVEL, but there are security vulnerabilities. Method Two: Use the Json.parse method to refer to JSON string parsing as a JSON objectJson.parse There is no security vulnerability, but there is compatibility, IE8 and not supported. :

Run results

 //create an object XMLHttpRequest          varajax=NewXMLHttpRequest (); //Create an HTTP request, set the request addressAjax.open ("Get", "testservlet.do"); //Send request (get is null post as parameter)Ajax.send (NULL); //set an event for Ajax (most 4[1-4 in this sense)Ajax.onreadystatechange =function(){            //5. Get the response            //ResponseText receive the information returned by the server as a string            //Console.log (ajax.readystate);            if(Ajax.readystate = = 4 && ajax.status = 200){                //use the Json.parse method to refer to JSON string parsing as a JSON object                vardata =Json.parse (Ajax.responsetext);                Console.log (Data.data); //Use the Eval method to parse a JSON string into a JSON object                varData1=eval ("(" +ajax.responsetext+ ")");            Console.log (Data1.data); }        }

Native Ajax receives JSON string (simple Introduction)

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.