AJAX cross domain call

前端 未结 11 1098
温柔的废话
温柔的废话 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 03:58

    You can use YQL to do the request without needing to host your own proxy. I have made a simple function to make it easier to run commands:

    function RunYQL(command, callback){
         callback_name = "__YQL_callback_"+(new Date()).getTime();
         window[callback_name] = callback;
         a = document.createElement('script');
         a.src = "http://query.yahooapis.com/v1/public/yql?q="
                 +escape(command)+"&format=json&callback="+callback_name;
         a.type = "text/javascript";
         document.getElementsByTagName("head")[0].appendChild(a);
    }
    

    If you have jQuery, you may use $.getJSON instead.

    A sample may be this:

    RunYQL('select * from html where url="http://www.google.com/"',
           function(data){/* actions */}
    );
    

提交回复
热议问题