AJAX cross domain call

前端 未结 11 1086
温柔的废话
温柔的废话 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:04

    I use this code for cross domain ajax call, I hope it will help more than one here. I'm using Prototype library and you can do the same with JQuery or Dojo or anything else:

    Step 1: create a new js file and put this class inside, I called it xss_ajax.js

    var WSAjax = Class.create ({
        initialize: function (_url, _callback){
            this.url = _url ;
            this.callback = _callback ;
            this.connect () ;
        },
        connect: function (){
            var script_id = null;
            var script = document.createElement('script');
            script.setAttribute('type', 'text/javascript');
            script.setAttribute('src', this.url);
            script.setAttribute('id', 'xss_ajax_script');
    
            script_id = document.getElementById('xss_ajax_script');
            if(script_id){
                document.getElementsByTagName('head')[0].removeChild(script_id);
            }
    
            // Insert 
    
                                     
                  
提交回复
热议问题