Using Jquery.ajax () to implement cross-domain

Source: Internet
Author: User

Cross-domain through jquery ajax, which is actually implemented using the JSONP way.

JSONP is an abbreviation for English json with padding. It allows script tags to be generated on the server side to return to the client, that is, to dynamically generate JavaScript tags and to read data in the form of JavaScript callback.

The front-end code is as follows:

  
 
  1. $.ajax({
  2. type:"get",
  3. async:false,
  4. url:"http://192.168.0.168:8080/lightview/filecentral/filePageNums.do?id=443d6058-8f2b-4caa-97c8-bdb542346745",
  5. dataType:"jsonp",
  6. jsonp:"jsoncallback",
  7. jsonpCallback:"success_jsoncallback",
  8. success:function(data){
  9. document.getElementById("spanTotalPageNum").innerHTML = data.pageNums;
  10. pageNumsMax = data.pageNums;
  11. }
  12. })

The server-side code is as follows:
Started to think, as long as the client through JSONP can directly cross-domain access, in fact, need server-side support

  
 
  1. @RequestMapping(value = "filePageNums.do",produces = "text/plain;charset=UTF-8")
  2. @ResponseBody
  3. pub Lic string Getfilepagenums Span class= "pun" > ( @RequestParam ( "id" ) string ID @RequestParam ( "Jsoncallback" ) string Callbackname ) {
  4. if(id==null||id.trim().equalsIgnoreCase("")){
  5. throw new NullPointerException();
  6. }
  7. FileScope fileScope=service.getFileById(id);
  8. String result=callbackName+"("+"{\"pageNums\":\"";
  9. if(fileScope==null){
  10. result+="0";
  11. }
  12. else {
  13. result+=String.valueOf(ViewFileAPI.viewFilePageNums(fileScope.getFileId()));
  14. }
  15. result+="\"})";
  16. return result;
  17. }

First register a callback (for example: ' Jsoncallback ') on the client, and then pass callback's name (for example: Success_jsonpcallback) to the server-side corresponding handler function.

The server becomes the JSON data that needs to be returned to the client. Then, in JavaScript syntax, a function is generated, and the function name is the value of the parameter (jsoncallback) passed up (Success_jsonpcallback).

Finally, the JSON data is placed directly into the function in the form of a parameter, so that a document of JS syntax is generated and returned to the client.
The client browser, parsing the script tag, and returning the server-side data as parameters,
Passed into the client's pre-defined callback function (as in the previous example, the jquery $.ajax () method encapsulates the Success:function (JSON)).

In fact, cross-domain loading data by dynamically adding script, unable to get data directly, so we need to use callback function.



From for notes (Wiz)

Using Jquery.ajax () to implement cross-domain

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.