A good way for JavaScript to interpret the JSON returned by Ajax

Source: Internet
Author: User

Typically, the AJAX request returns the format JSON or XML, and if it returns JSON, it can be manipulated by converting it to a JavaScript object, as follows:

1, the controller implementation of the AJAX request

@RequestMappingpublic void Getlocations (@RequestParam String location, PrintWriter printwriter) {if ( Stringutils.isempty (location)) {return;} list<location> locations = locationservice.getsublocation (location); String json = Json.tojson (locations);p Rintwriter.write (JSON);p Rintwriter.flush ();p rintwriter.close ();}

A location is a bean that contains multiple properties, such as PName, Zname.

2. AJAX processing requests and return values

$.ajax ({type: "GET", url: "/admin/location/getlocations.do", Data: "location=" + val,success:function (msg) {msg = eval ( msg); region = $ ("#region"); Region.empty (); vHtml = "<option value= ' None ' > select area (optional) </option>"; $.each (MSG, function (i) {var $bean = msg[i];vhtml + = ' <option value= "' + $bean. PName + '" "> ' + $bean. Zname + ' </option> ';} ); region.html (vHtml);}});

MSG is originally a JSON string that uses the Eval function to turn a string into a JavaScript object, so that you can get the property value like an object.

  

A good way for JavaScript to interpret the JSON returned by Ajax

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.