Ajax Request Code:
Zone Event Select the distribution point function changedistrict (value) {if (value = = 0) {$ (' #transport_node '). empty (); $ (' #transport_node '). Append (' <option value= "0" > Please select </option> '); Return } $ (' #transport_node '). empty (); $ (' #transport_node '). Append (' <option value= "0" > Load in ...</option> '); $.ajax ({type: ' GET ', url: "Http://192.168.33.114:8080/UIDTraceAdmin/transportnode/pagelist/jsonp?callback Function=jsonpcallback ", Async:false, DataType:" Jsonp ", Jsonp:" Jsonpcallback ",//passed to the request handler or page, with To obtain the parameter name of the JSONP callback function name (generally by default: callback) Success:function (o) {}, timeout:3000}); }function Jsonpcallback (Result) {if (result.success = = True) {$ (' #transport_node '). empty (); $ (' #transport_node '). Append (' <option value= "0" > Please select </option> '); $.each (result.transportlist, function (i, n) {var html = ' <option value= ' ' +n.nodeid+ ' "> ' +n.transportnodename+ ' </option> '; $ (' #transport_node '). Append(HTML)}); }else{$ (' #transport_node '). empty (); $ (' #transport_node '). Append (' <option value= "0" > Please select </option> '); } }
Points:
Request parameters in 1.url Callbackfunction=jsonpcallback This is the name of the JavaScript method that is called after the server response is returned
2.dataType for Jsonp
To process code across domain servers:
@RequestMapping ("/pagelist/jsonp") public void PageList (@ModelAttribute transportnode node,httpservletrequest Httpreq,httpsession session,httpservletresponse response) {//Return head settings Response.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); Response.setheader ("Content-type", "application/x-javascript;charset=utf-8"); Response.setdateheader ("Expires", 0); String jsonpcallback = Httpreq.getparameter ("callbackfunction");//jsonp callback function name Jsonobject Resultjson = new JSONObje CT (); PrintWriter out = null; try {out = Response.getwriter ();} catch (IOException E1) {e1.printstacktrace ();} try {Node.setrowstart (Node.getpage ()-1) * Node.getrows () + 1); Node.setrowend (Node.getpage () * node.getrows ()); Resultjson.put ("Transportlist", Jsonutils.tojsonlist (business.getlist (node))); Resultjson.put ("Success", true); System.out.println (Resultjson.tostring ()); Out.println (jsonpcallback+ "(" +reSultjson.tostring () + ")");//return JSONP Format Data Out.flush (); Out.close (); } catch (Exception e) {LogWriter.log ("/pagelist/jsonp", e); try {resultjson.put ("success", false);} catch (Jsonexception E1) {e1.printstacktrace ();} Out.println (jsonpcallback+ "(" +resultjson.tostring () + ")");//return JSONP Format Data Out.flush (); Out.close (); }
Note The main points:
1. Set the response packet header, Response.setheader ("Content-type", "Application/x-javascript;charset=utf-8"), and eliminate the "Resource interpreted As Script but transferred with MIME type Text/plain ", set the correct encoding according to your own encoding format;
The 2.JSONP data format is: jsonpcallback+ "(" +resultjson.tostring () + ")"
As an example:
Jsonpcallback ({
"Code": "AAA",
"Price": 1780,
"Tickets": 5
});