Javascript XMLHttpRequest“网络错误”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18420982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:50:45  来源:igfitidea点击:

Javascript XMLHttpRequest "NetworkError"

javascripthttpgoogle-chromesingle-sign-onput

提问by shaddow

I am inexperienced in javascript and web development in general. The project I am working on is part of a general company training program. We have been instructed to use Google Chrome as the primary testing browser.

我一般没有 javascript 和 web 开发经验。我正在从事的项目是一般公司培训计划的一部分。我们已被指示使用 Google Chrome 作为主要测试浏览器。

Essentially, I am writing an application that will run on clients external to the company intranet. Connections will pass through our single sign on server to internal services. The single sign on service is mostly transparent to the server-side application - it intercepts requests to internal services and prompts for credentials if necessary. From what I can tell, it changes the session cookie during every request sent.

本质上,我正在编写一个应用程序,该应用程序将在公司内部网外部的客户端上运行。连接将通过我们的单点登录服务器传递到内部服务。单点登录服务对服务器端应用程序来说大多是透明的——它拦截对内部服务的请求,并在必要时提示输入凭据。据我所知,它会在每次发送请求期间更改会话 cookie。

I have established connections to services using HTTP GET and POST connections. However, there is one service that requires an HTTP PUT request. This is where the trouble is coming in.

我已经使用 HTTP GET 和 POST 连接建立了到服务的连接。但是,有一项服务需要 HTTP PUT 请求。这就是麻烦的地方。

When the script is run, the Chrome javascript console gives the following error:

当脚本运行时,Chrome javascript 控制台给出以下错误:

Uncaught NetworkError: A network error occurred.

The error is so vague. When I look at the Network tab in the Chrome developer tab, it shows the request, but it does not have any response data.

这个错误太模糊了。当我查看 Chrome 开发人员选项卡中的网络选项卡时,它显示了请求,但没有任何响应数据。

For curiosity, I right clicked on the request and copied it as a cURL request. I appended the session cookie to the request via the -b flag. cURL returns a HTTP 302, with a reference to a make cookie script on the server. Being a PUT request, doesn't any redirect cause the request to fail? But I'm not positive this is what is happening in Chrome/js.

出于好奇,我右键单击该请求并将其复制为 cURL 请求。我通过 -b 标志将会话 cookie 附加到请求中。cURL 返回 HTTP 302,并引用服务器上的 make cookie 脚本。作为 PUT 请求,是否有任何重定向导致请求失败?但我并不肯定这就是 Chrome/js 中发生的事情。

Is there a way I can get more information about the error?

有什么方法可以获得有关错误的更多信息?

I am not 100% confident in the team that developed the internal services for us to use, so the use of a PUT request could indeed be impossible... the services are also still in development.

我对开发内部服务供我们使用的团队不是 100% 有信心,所以使用 PUT 请求确实是不可能的......这些服务还在开发中。

To be clear, the overall problem is that I need this PUT request to go through our SSO application, if it is possible.

需要明确的是,总体问题是我需要这个 PUT 请求通过我们的 SSO 应用程序,如果可能的话。

回答by Matt

I've run into the same problem. It seems to only happen on redirect responses. If you set your async flag to false and use a callback function then the error goes away. For example:

我遇到了同样的问题。它似乎只发生在重定向响应上。如果您将异步标志设置为 false 并使用回调函数,那么错误就会消失。例如:

xmlhttp.open("GET","http://google.com", false);

Then you'll need a function like this to listen for the response:

然后你需要一个这样的函数来监听响应:

xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    // do stuff here
  }
}