JavaScript Cross Domain

一、jsonp

jQuery jsonp:

0> 动态script

var script = document.createElement('script');
script.type = 'text/javascript;
script.src = 'http://......?function_defined_on_server=my_callback_function';
document.head.appendChild(script);

function my_callback_function(data){
    // ......        
}

 

1>

$.ajax({

        url: "http://.......",

        type: 'GET',

        dataType: 'JSONP',//here

        success: function (data) {

 

        }

});

 
2>
$.getJSON("http://......", function(data) {
});
 
3>

$.ajax({

        url: "http://.......",

        type: 'GET',

        dataType: 'JSON',

        crossDomain: true, //default: false for same-domain requests, true for cross-domain requests

})

.done(function() {
  alert( "success" );
})
.fail(function() {
  alert( "error" );
})
.always(function() {
  alert( "complete" );
});

 


 

二、服务器代理

在服务器端向站外资源发起请求,然后将得到的数据提供给本站的客户端。

 

三、

 

posted @ 2017-03-28 22:44  hexiaoco  阅读(369)  评论(0)    收藏  举报