CORS同步请求在Firefox中不工作 [英] CORS synchronous requests not working in firefox

查看:180
本文介绍了CORS同步请求在Firefox中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery的官方文档(异步ajax部分)说:

The official documentation of jQuery ( async ajax section ) says that:


跨域请求和dataType:jsonp请求不支持
同步操作。

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

但是这在所有最近的浏览器,但firefox版本> = 20.这是我打电话的类型:

However this works in all recent browsers but firefox version >= 20. Here is the type of calls i'm making:

$.ajax({
      type : "GET",
      async: false,
      dataType : "text",
      url : link,
      xhrFields: { withCredentials: true },

      success: function(response){
         console.log("success ");
      },
        error: function(error){
            console.error(error);               
        }  
});

有没有人知道为什么会发生这种情况?

Does anyone have a clue why this is happening ?

UPDATE:
我使用jQuery和vanilla XHR测试的错误总是相同的。

UPDATE: Ive tested both with jQuery and vanilla XHR the error is always the same


[Exception ...
底层对象不支持参数或操作代码:15nsresult:0x8053000f
(InvalidAccessError)

[Exception... "A parameter or an operation is not supported by the underlying object" code: "15" nsresult: "0x8053000f (InvalidAccessError)"


推荐答案

使用 beforeSend 代替 xhrField

$.ajax({
    type : "GET",
    async: false,
    dataType : "text",
    url : link,
    beforeSend: function(xhr) {
      xhr.withCredentials = true;
    },
    success: function(response){
      console.log("success ");
    },
    error: function(error){
      console.error(error);               
    }
});

这篇关于CORS同步请求在Firefox中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆