Third job-front-end and background data exchange issues

Source: Internet
Author: User
Tags javascript eval

Question: What are the JSON data methods that the JS frontend obtains and resolves back to the backend server ?

Some time ago, I was making a Web Appabout food, a lot of business store information and some user comments and other content needs to get from the background, if just static layout is too rigid, but in the front-end and back-end data exchange encountered a lot of problems, such as the topic JS front-end to obtain and parse the backend server back the JSON data returned by the method is one of the problems.

Before solving this problem, you first need to understand the JavaScript eval () function, theeval (string) function evaluates a string, and executes the JavaScript Code.

JS Frontend gets and parses the JSON data returned by the backend server:

1. Using the eval function to parse JSON

Assume the JSON string returned by the server:

Var Data= "

{root:

[{name: ' 1 ', Value: ' 0 '},

{name: ' 6101 ', Value: ' Beijing '},

{name: ' 6101 ', Value: ' Tianjin '},

{name: ' 6101 ', Value: ' Shanghai City '},

{name: ' 6101 ', Value: ' Chongqing '}

]}”;

If the jquery asynchronous request does not have a type description, or is accepted as a string, an object processing is required, and the JSON string is placed on the eval () executed once in:

var dataobj=eval ("(" +data+ ")"); convert to JSON object

Note: Theeval parameter is written in the form of "(" +data+ ")" This type of parenthesis. " The purpose of the parentheses is to force the eval function to force the expression in parentheses when it is handling JavaScript code ) to an object instead of being executed as a statement (statement).

2. Using the $.getjson () method

json jquery The asynchronous request will type set to JSON, or use $.getjson () method to get the server back, then you do not need eval () method, Because the result is already json object, Just call the object directly, as follows:

$getJSON ("http://www.exp99.com/", {param: "User_guid"},function (data) {//The data returned here is already JSON Object

$.each (Data.root,function (Idx,item) {

if (idx==0) {

Return ture;// with continue, returns false with break }

Console.log ("Name:" +item.name+ ", Value:" +item.value);

});

});

Third job-front-end and background data exchange issues

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.