Python 错误选项 net::ERR_CONNECTION_REFUSED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45115309/
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-08-19 16:42:16  来源:igfitidea点击:

Error OPTIONS net::ERR_CONNECTION_REFUSED

javascriptjquerypythongoogle-chrome

提问by Purander Dua

I am developing a web application using "jQuery"(front-end) and "Python"(back-end). While making a PUT request to update details in the database, this is the error I get in the console:

我正在使用“jQuery”(前端)和“Python”(后端)开发 Web 应用程序。在发出 PUT 请求以更新数据库中的详细信息时,这是我在控制台中得到的错误:

OPTIONS "REST API URL" net::ERR_CONNECTION_REFUSED

选项“REST API URL”网络::ERR_CONNECTION_REFUSED

My jQuery code is:

我的 jQuery 代码是:

$.ajax({ 
    type: "PUT",
    url: "REST API URL",
    headers: {"Content-Type": "application/json", "Authorization": AuthToken},
    data: "details to be updated in database",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data,status) {
      //do something with data
    },
    error: function(data,status) {
      //show error data and status
    }
)};

I read about how HTTP Requests other than GETand POSTare first pre-flighted as OPTIONS request and only when it is a genuine request, it gets processed as a PUT/DELETE/PATCHrequest.
I saw solutions where it said that it might be a CORSissue, but CORSis enabledfrom the back-end to allow GET/POST/PUT/PATCH/DELETErequests. Further, I am successfully able to make GETand POSTrequests but no PUTrequests are going through.
I am using "Chrome Dev Tools" and researched about how to fix this error for Chrome by clearing cache and cookies, flushing DNS and re-installing Chrome but none of the solutions have worked so far.
I am a making the front end UI and am not sure whether this is a client-side error or a server-side error?
Any help would be appreciated.

我读到HTTP以外如何请求GETPOST先预排期为OPTIONS请求,只当它是一个真正的请求时,它就会被处理为一个PUT/DELETE/PATCH请求。
我看到的解决方案在那里说,这可能是一个CORS问题,但CORS启用从后端允许GET/POST/PUT/PATCH/DELETE请求。此外,我能够成功地提出GETPOST请求,但没有PUT请求通过。
我正在使用“Chrome 开发工具”并研究了如何通过清除缓存和 cookie、刷新 DNS 和重新安装 Chrome 来解决 Chrome 的这个错误,但到目前为止没有一个解决方案有效。
我正在制作前端 UI,但不确定这是客户端错误还是服务器端错误?
任何帮助,将不胜感激。

回答by Maurice Meyer

Given the fact it is really a CORS issue - browsers 'preflight' the request using OPTIONS method. After the OPTIONS request succeedsthe actual request (in your case PUT) is made.

鉴于这确实是一个 CORS 问题 - 浏览器使用 OPTIONS 方法“预检”请求。在 OPTIONS 请求成功后,实际请求(在您的情况下是 PUT)被发出。

Make sure, the backend responds to OPTION requests. You could easily catch all OPTION requests and return 200 OKor 204 NO CONTENT.

确保后端响应 OPTION 请求。您可以轻松捕获所有 OPTION 请求并返回200 OKor 204 NO CONTENT

回答by EyoelD

One thing is for sure, this is a backend problem. This happens when the cross origin communication between the backend and frontend is not connected properly. Considering you have imported cors and set up the middleware, most probably you have made a mistake using the PUT method in terms of the origin URL and request URL.

有一件事是肯定的,这是一个后端问题。当后端和前端之间的跨源通信未正确连接时,就会发生这种情况。考虑到您已经导入了 cors 并设置了中间件,很可能您在使用 PUT 方法的原始 URL 和请求 URL 方面犯了错误。

Things you can do:

你可以做的事情:

1) Make sure both servers are running (the back-end and front end).

1) 确保两台服务器都在运行(后端和前端)。

2) Look into google development tool and see the network section. Look at the request headers and the general. Make sure the request URL / backendhas your backend server URL and the orgin / frontendhas your frontend URL.

2)查看谷歌开发工具并查看网络部分。查看请求头和一般。确保请求 URL / 后端具有您的后端服务器 URL,并且源/ 前端具有您的前端 URL。

3) Make sure in your http.put() method, the domain you are feeding it matches the api you set up in your server.

3) 确保在您的 http.put() 方法中,您提供给它的域与您在服务器中设置的 api 匹配。

4) Your issue is that your backend is not connected with your front end properly,so don't waste your time trying to find other errors. Focus on debuging the http.put() method and the cors module and middleware you have imported.

4) 你的问题是你的后端没有正确连接你的前端,所以不要浪费你的时间去寻找其他错误。重点调试您导入的 http.put() 方法和 cors 模块和中间件。