最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery ajax 跨域插件jquery.xdomainrequest.min.js的使用方法

 更新時(shí)間:2023年06月18日 13:07:15   作者:七汐愛椰棗  
jQuery XDomainRequest 是一個(gè)利用 XDomainRequest 對象為 IE8、IE9 實(shí)現(xiàn)跨域資源共享(CORS - Cross Origin Resource Sharing)的 jQuery 插件

jQuery XDomainRequest 是一個(gè)利用 XDomainRequest 對象為 IE8、IE9 實(shí)現(xiàn)跨域資源共享(CORS - Cross Origin Resource Sharing)的 jQuery 插件。

官方網(wǎng)址:https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

約束:

1. 使用 $.ajax 發(fā)送請求,jQuery 版本需在 1.5+
2. 服務(wù)端需設(shè)置 header:header('Access-Control-Allow-Origin:http://angular.js');
3. 請求方式僅限:GET / POST
4. 協(xié)議僅限:HTTP / HTTPS,且必須相同
5. 僅限異步請求

經(jīng)驗(yàn):

服務(wù)端設(shè)置 header('Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS'); 時(shí),各瀏覽器跨域支持情況有差異。

With at least jQuery version 1.5, just include the jquery.xdomainrequest.min.js script into your page, then make your AJAX call like you normally would:

// GET
$.getJSON('http://jsonmoon.jsapp.us/').done(function(data) {
  console.log(data.name.first);
});
// POST
$.ajax({
  url: 'http://frozen-woodland-5503.herokuapp.com/cors.json',
  data: 'this is data being posted to the server',
  contentType: 'text/plain',
  type: 'POST',
  dataType: 'json'
}).done(function(data) {
  console.log(data.name.last);
});

更新說明

1.0.2 - added RequireJS AMD module support

1.0.3 - added CommonJS and Bower support

1.0.4 - support protocol-relative URLs, use peerDependencies in package.json

應(yīng)用實(shí)例

代碼:

api.php:

<?php
    // 指定可信任的域名來接收響應(yīng)信息
    header('Access-Control-Allow-Origin:http://angular.js');
    // 允許攜帶 用戶認(rèn)證憑據(jù)(也就是允許客戶端發(fā)送的請求攜帶Cookie)
    header('Access-Control-Allow-Credentials:true');
    //header('Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS');
    //header('Access-Control-Allow-Headers:Content-Type, Authorization, Content-Length, X-Requested-With, Accept, x-csrf-token, origin');
    $type = $_SERVER['REQUEST_METHOD'];
    parse_str(file_get_contents('php://input'), $data);
    $data = array_merge($_GET, $_POST, $data);
    echo json_encode(array(
        'type' => $type,
        'data' => $data['data']
    ));
?>

ajax.html:

<script src="http://libs.cncdn.cn/jquery/1.11.1/jquery.min.js"></script>
<script src="http://libs.cncdn.cn/jquery-ajaxtransport-xdomainrequest/1.0.3/jquery.xdomainrequest.min.js"></script>
<script>
  $.ajax({
    url: 'http://ndol.cn/api.php',
    data: {
      'data': 'zhao'
    },
    type: 'POST',
    dataType: 'json'/*,
    xhrFields:{
      withCredentials:true
    }*/
  }).done(function(data) {
    alert(JSON.stringify(data));
  });
  //alert($.support.cors);
</script>

其它網(wǎng)友的補(bǔ)充

IE8&IE9上不能使用XMLHttpRequest來通過cors來處理跨域,他們提供了一個(gè)特別的對象XDomainRequest來處理CORS跨域通訊。

可以參考:CORS

它的回掉函數(shù)和XMLHttpRequest基本一致,這里不重?cái)ⅰ?/p>

下面只要說幾個(gè)坑點(diǎn):

1.要保持請求協(xié)議和當(dāng)前訪問網(wǎng)站的協(xié)議一致。

     例如:瀏覽器地址欄的協(xié)議是https,那么你的XDomainRequest只能發(fā)起https請求,否則“拒絕訪問”

2. 如果你使用vue同時(shí)使用CORS,想要兼容IE8!不用多想了。

   XDomainRequest是支持IE8 CORS的,但是vue不支持,這個(gè)鍋并不是XDomainRequest的,XDomainRequest不背(●'?'●)。

貼代碼:

return new Promise((resolve, reject) => {
            let appliance = new window.XDomainRequest()
            appliance.onprogress = function () { }; // no aborting
            appliance.ontimeout = function () {
               // alert("timeout")
                reject({ eror: "timeout" });
            }; // "
            appliance.onload = function (e) {
                // do something with appliance.responseText
               // alert("onload" + appliance.responseText)
                resolve(appliance.responseText);
            };
            appliance.onerror = function (e, b) {
                // error handling
               // alert("error" + JSON.stringify(e) + JSON.stringify(b))
                reject({ eror: e });
            };
            //appliance.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            appliance.withCredentials = true; // to support sending cookies with CORS
            appliance.open("POST", axios.defaults.baseURL + url);
            appliance.send(dataToString);
        });

到此這篇關(guān)于jQuery ajax 跨域插件jquery.xdomainrequest.min.js的使用方法的文章就介紹到這了,更多相關(guān)jQuery XDomainRequest內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

如皋市| 中方县| 确山县| 栖霞市| 兴国县| 江北区| 湖北省| 富川| 青海省| 岳池县| 曲周县| 旺苍县| 叶城县| 新昌县| 南岸区| 洛宁县| 文昌市| 梓潼县| 孝感市| 宜章县| 大关县| 潼关县| 康马县| 松潘县| 海阳市| 乌审旗| 德令哈市| 普洱| 宜良县| 老河口市| 辰溪县| 陈巴尔虎旗| 当阳市| 文水县| 子洲县| 无为县| 康乐县| 那坡县| 淮南市| 若羌县| 静乐县|