jQuery 跨域請求post

來源:互聯網
上載者:User

標籤:

基於同源策略安全性跨域會有阻止。
服務端指定返回
header("Access-Control-Allow-Origin: 只能指定具體網域名稱不能*");
header("Access-Control-Allow-Credentials: true");

跨網域設定cookie開啟P3P
header(‘P3P: CP=" CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR "‘);


下面適用於非IE,可以實現跨域
$.ajax({url:api_url + _url,data:_params,dataType:(_type ? "json" : "jsonp"),type:(_type ? "POST" : "GET"),crossDomain:true,xhrFields:{withCredentials:true},success:function(list){if(list.Result == true){ if (_callback){ var func = TM[_callback];if (typeof func == "function") {func(list.Data);} }}else{if(list.Msg){alert(list.Msg);}else{alert("操作失敗");}}},error:function(){alert("網路錯誤,請稍後重試");}});

 

http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/

IE:針對IE8 IE9

function createCORSRequest(method, url){    var xhr = new XMLHttpRequest();    if ("withCredentials" in xhr){        xhr.open(method, url, true);    } else if (typeof XDomainRequest != "undefined"){        xhr = new XDomainRequest();        xhr.open(method, url);    } else {        xhr = null;    }    return xhr;}var request = createCORSRequest("get", "http://www.nczonline.net/");if (request){    request.onload = function(){        //do something with request.responseText    };    request.send();}

 

其他方法:

           
               

There is no way except to include the authentication cookie value / token in the query string e.g. :

 buy.api.example.com/?sessionId=$sessionId&otherparameters=test 
and set your webservice to check the query string if cookies are not present.

 

 

jQuery 跨域請求post

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.