【问题标题】:XMLHttpRequest POST and FirefoxXMLHttpRequest POST 和 Firefox
【发布时间】:2022-09-27 20:42:04
【问题描述】:

我有一个带有 javascript 的网页,它将 json 数据发布到 python 烧瓶应用程序。 在 Chrome、Edge、Opera、Android、a.s.o 上一切正常。 只是 Firefox 给了我一个错误。

这是我的 JavaScript:

  const xhr = new XMLHttpRequest();

  xhr.open(\"POST\", url);
  xhr.setRequestHeader(\"Content-Type\", \"application/json;charset=UTF-8\");
  xhr.setRequestHeader(\"Authorization\", authdata);

  xhr.onreadystatechange = function(ev) {
     //2 - request sent, 3 - something back, 4 - full response
     //console.log(xhr.readyState);
     if (xhr.readyState === 4) {
        switch (xhr.status) {
        case 200:
        case 304:
          console.log(\"OK or Not Modified (cached)\", xhr.status);
          console.log(xhr.responseText);
          break;
        case 201:
          console.log(\"Created\", xhr.status);
          console.log(xhr.responseText);
          break;
        case 400:
          console.log(\"Bad Request\", xhr.status);
          alert(\"Bad Request\");
          break;
        case 401:
        case 403:
          console.log(\"Not Authorized or Forbidden\", xhr.status);
          alert(\"Not Authorized or Forbidden\");
          break;
        case 404:
          console.log(\"Not Found\", xhr.status);
          alert(\"404 Not Found\");
          break;
        case 500:
          console.log(\"Server Side Error\", xhr.status);
          alert(\"Server Error 01 Code: \" + xhr.status.toString());
          break;
        default:
          console.log(\"Some other code: \", xhr.status);
          alert(\"Server Error 02 Code: \" + xhr.status.toString());
        }
    }
 };

 xhr.onerror = function(err) {
   console.warn(err);
   alert(\"Server Error 99\", err);
 };

 edata = JSON.stringify({ \"domain\": \"workdomain\", \"zonedata\": \"data\" });
 xhr.send(edata);

当我在 Firefox 中触发它时,我得到一个 \"Server Error 02\" 状态代码 0 如果我查看调试器网络选项卡,则没有 POST 发送。

这是火狐的问题吗?

  • \"这是 Firefox 的问题吗?\"- 在 2022 年仍然使用 XMLHttpRequest 而不是 fetch,这可能是您在开发人员方面可以称之为问题的问题 :-)
  • 不确定这是否是 Firefox 问题 - 我从未见过错误代码 02 - 哦,等等,那是您的代码在执行此操作...
  • developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status:\"如果出现 XMLHttpRequest 错误,浏览器也会报告状态为 0。\"- 您在浏览器控制台中遇到任何其他错误?这是跨域/受CORS影响吗?
  • urlhttp://.....? https://....?别的东西?
  • 没有其他错误。网址是10.10.0.43:5555/someroute。 CORS 不是问题。所有其他浏览器都成功发送帖子。

标签: javascript firefox


【解决方案1】:

玩了一段时间后,我发现这是 Firefox 中的时间问题。

我在发送语句中添加了一个 setTimeout,一切运行顺利。

这是现在有效的代码:

     const xhr = new XMLHttpRequest();
     xhr.open("POST", url);
     xhr.timeout = 0;
     xhr.setRequestHeader("Authorization", authdata);
     xhr.setRequestHeader("Content-Type", "application/json");
     xhr.setRequestHeader("Accept", "*/*");

     setTimeout(function() {
        xhr.send(edata);
     }, 1000);

     xhr.onerror = function(err) {
       console.warn(err);
       alert("Server Error no response", err);
     };

【讨论】:

    猜你喜欢
    • 2011-05-15
    • 1970-01-01
    • 2022-01-17
    • 2011-12-29
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 2015-11-11
    相关资源
    最近更新 更多