The perfect solution for jquery Ajax cross-domain (Jsonp method)

Source: Internet
Author: User

Ajax cross-domain request problem, jquery for Ajax cross-domain request there are two types of solutions, but all are only supported get mode, next for you to detail the next client Jquery.ajax call code Today in the project need to do remote data loading and rendering the page, until the development stage to realize the problem of Ajax cross-domain requests, vaguely remember that jquery has mentioned an Ajax cross-domain request solution, and immediately turned out the jquery API out of the research, sent
jquery has two types of solutions for Ajax cross-domain requests, but only the Get method is supported. The Jquery.ajax Jsonp format and Jquery.getscript method of jquery are respectively.

What is the JSONP format? API Original: If the obtained data file is stored on the remote server (the domain name is different, that is, cross-domain Fetch data), you need to use the JSONP type. With this type, a query string parameter callback= is created? , this parameter is appended to the URL of the request. The server side should precede the JSON data with a callback function name in order to complete a valid JSONP request. This means that the remote server needs to do the processing of the returned data, according to the callback parameters submitted by the client, return a callback (JSON) data, and the client will process the return data in script to do the JSON data. Jquery.getjson also supports JSONP data-mode invocation.

1$.ajax ({
2url:basepath+ "/login/login?msg_base64=" +Msg_base64,3Asyncfalse,4 //data:{5 //"formcontent": STR,6 //"FormName": FormName,7 //"leixing": leixing8 //                    },9DataType: "Jsonp",TenProcessData:false,      OneType: "Get", ASuccessfunction(data) { -Data=data[0]; - alert (data); the                         if(data.status== "0"){ -$ ("#usererror"). HTML (data.msg); -}Else if(data.status== "1"){ -$ ("#pwderror"). HTML (data.msg); +                             } -                         Else if(data.status== "2"){ +Alert ("Utility bill:" +data.data); AAddcookie ("token", data.data,1);//depositing tokens in a cookie atwindow.location.href= "Edit.html"; -                             } -                     } -});
1@ResponseBody//automatically converts the return result of a method to a JSON format string, noting that the package that needs to guide the JSON2@RequestMapping (value = "/login", method =requestmethod.get)3      PublicString Login (httpservletrequest request,string msg_base64) {4         5         6String Callback=request.getparameter ("callback");7         //System.out.println (callback);8                                                                 //Information9 //String msg_context_base64 = Msg_base64.split ("") [1];TenString msg = Noteutil.base64decode (msg_base64);//decryption, decryption: Username:password OneString name = Msg.split (":") [0]; AString pwd = Msg.split (":") [1]; -Noteresult result =Service.checkuser (name, pwd); -SYSTEM.OUT.PRINTLN ("result" +result.tostring ()); theJsonarray json=jsonarray.fromobject (result); -System.out.println ("+callback+" ("+json+") "); -         returnCallback+ "(" +json.tostring () + ")"; -     } +

The perfect solution for jquery Ajax cross-domain (Jsonp method)

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.