Ajax cross-domain request--JSONP instances

Source: Internet
Author: User


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
});





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.