AJAX cross domain call

前端 未结 11 1151
温柔的废话
温柔的废话 2020-11-22 03:43

I know about AJAX cross-domain policy. So I can\'t just call \"http://www.google.com\" over a ajax HTTP request and display the results somewhere on my site.

I tried

11条回答
  •  佛祖请我去吃肉
    2020-11-22 04:11

    You will need to dynamically insert a script tag into the page that references the data. Using JSONP, you can execute some callback function when the script has loaded.

    The wikipedia page on JSONP has a concise example; the script tag:

    
    

    would return the JSON data wrapped in a call to parseResponse:

    parseResponse({"Name": "Cheeso", "Rank": 7})
    

    (depending on the configuration of the getjson script on domain1.com)

    The code to insert the tag dynamically would be something like:

    var s = document.createElement("script");
    s.src = "http://domain1.com/getjson?jsonp=parseResponse";
    s.type = "text/javascript";
    document.appendChild(s);
    

提交回复
热议问题