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