【问题标题】:CORS synchronous requests not working in firefoxCORS 同步请求在 Firefox 中不起作用
【发布时间】:2013-05-16 02:59:36
【问题描述】:

jQuery 的官方文档(async ajax section)说:

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

但是,这适用于所有最近的浏览器,但 firefox 版本 >= 20。这是我正在进行的调用类型:

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

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

有人知道为什么会这样吗?

更新: 我用 jQuery 和 vanilla XHR 测试过,错误总是一样的

[Exception...“参数或操作不受 底层对象”代码:“15” nsresult:“0x8053000f (InvalidAccessError)"

【问题讨论】:

  • 你没有使用 dataType: "jsonp",所以它一定是跨域的部分,但是你没有告诉我们这个请求是从哪里/到哪里发出的。
  • FF 中发生了什么?网络检查员会告诉你什么?
  • @tandrewnichols 我正在使用 CORS 来发出跨域请求。
  • @Bergi 在网络检查器选项卡中没有显示任何请求。
  • 我在使用 CORS 时需要同步请求,如果我删除 async:false 请求成功。

标签: javascript jquery firefox cross-domain cors


【解决方案1】:

使用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);               
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 2023-03-04
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多