Thoughts on AJAX return values

Source: Internet
Author: User

Ajax requests. After the request, we need to get the return value, or one or more, and then perform the logic judgment based on the return value. Then, if we only have a single request, if a value is returned, we can directly receive it and then determine the processing, for example:

Success: function (data) {If (Data = What) {// how is it} else {// how is it }}

 

If an Ajax request needs to get multiple return values, since multiple values are returned, the problem of receiving and parsing multiple values at the front end is involved. It seems that we have met this problem, JSON is one of the best processing methods. Whether it is two values, three values, and ten values, JSON is always so easy to use and received by the front-end. For example:

String jsons = "{'data1':" + 1 + ", 'data2 ':" + 2 + "}"; // This is a simple JSON string in the background. Note that, is of the string type. // At the front end, I want to obtain the value of data1, the value of data2, but this is a string. What I get at the front end is also a string, as shown in the following: Success: function (data) {alert (data ); // This is a string}

Data. data1 and data. data2 are both undesirable, because we don't know each other at all. At this time, we have another function: eval ()
// Eval (string) only receives the original string data and performs operations on the string. As the name suggests, the colon data1: 1 in our JSON format has been treated as an operator, this is understandable. as follows:

Success: function (data) {var jsons = eval ("(" + Data + ")"); alert (jsons. data2); alert (jsons. data1 );}

In this way, the front-end can receive multiple values returned by the background JSON.

 

 

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.