關於jQuery中$.ajax()跨域

今天研究了一些用jquery $.ajax跨域的方法,總結了一些注意事項以下:jquery

     首先,跨域JSONP不是AJAX。它不使用XMLHttpRequest。只不過是一個動態腳本元素加載JavaScript代碼。   ajax

Cross-domain JSONP isn’t AJAX at all. It doesn’t use XMLHttpRequest. It’s nothing more than a dynamic script element that loads JavaScript code.json

You can’t do a POST with a dynamic script element. Where would you put the POST data?跨域

I don’t know what the $.ajax code is trying to do – maybe it should fail in a more informative way. It will fail one way or another regardless.app

 

     其次,要使用get訪問(type:get)。less

 

     第三,async要爲true(默認爲true) 。dom

       Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.async

 

     第四,cache要設爲false(默認爲true)。jsonp

       Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.code

 

     最後設置一下jsonp和jsonpCallback就ok了。

相關文章
相關標籤/搜索