SoFunction
Updated on 2025-03-01

A good way to explain json returned by ajax (recommended)

Usually the format returned by ajax request is json or xml. If the returned json, it can be converted into a javascript object, as follows:

1. Controller implementation of ajax request

@RequestMapping
public void getLocations(@RequestParam String location, PrintWriter printWriter) { 
  if ((location)) { 
    return; 
  } 
  List<Location> locations = (location); 
  String json = (locations); 
  (json); 
  (); 
  (); 
} 

Location is a bean containing multiple attributes, such as pName and zName.

2. Ajax handles request and returns value

$.ajax({ 
  type : "GET", 
  url : "/admin/location/", 
  data : "location=" + val, 
  success : function(msg) { 
    msg = eval(msg); 
    region = $("#region"); 
    (); 
    vHtml = "&lt;option value='none'&gt;Select area(Optional)&lt;/option&gt;"; 
    $.each(msg, function(i) { 
      var $bean = msg[i]; 
      vHtml += '&lt;option value="' + $ + '""&gt;'
          + $ + '&lt;/option&gt;'; 
    }); 
    (vHtml); 
  } 
}); 

msg is originally a json string. It uses the eval function to convert the string into a javascript object, so that the property value can be obtained like an object.

The above Javascript explanation of json returned by ajax is the best way (recommended) of the editor to share with you. I hope you can give you a reference and I hope you can support me more.