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