简体   繁体   English

错误选项 net::ERR_CONNECTION_REFUSED

[英]Error OPTIONS net::ERR_CONNECTION_REFUSED

I am developing a web application using "jQuery"(front-end) and "Python"(back-end).我正在使用“jQuery”(前端)和“Python”(后端)开发 Web 应用程序。 While making a PUT request to update details in the database, this is the error I get in the console:在发出 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 GET and POST are first pre-flighted as OPTIONS request and only when it is a genuine request, it gets processed as a PUT/DELETE/PATCH request.我读到了除了GETPOST之外的 HTTP 请求如何首先作为 OPTIONS 请求进行预检,并且只有当它是真正的请求时,它才会作为PUT/DELETE/PATCH请求进行处理。
I saw solutions where it said that it might be a CORS issue, but CORS is enabled from the back-end to allow GET/POST/PUT/PATCH/DELETE requests.我看到的解决方案它说,它可能是一个CORS问题,但CORS从后端启用允许GET/POST/PUT/PATCH/DELETE请求。 Further, I am successfully able to make GET and POST requests but no PUT requests are going through.此外,我能够成功地发出GETPOST请求,但没有通过PUT请求。
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.我正在使用“Chrome 开发工具”并研究了如何通过清除缓存和 cookie、刷新 DNS 和重新安装 Chrome 来解决 Chrome 的这个错误,但到目前为止没有一个解决方案有效。
I am a making the front end UI and am not sure whether this is a client-side error or a server-side error?我正在制作前端 UI,但不确定这是客户端错误还是服务器端错误?
Any help would be appreciated.任何帮助,将不胜感激。

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. 2)查看谷歌开发工具并查看网络部分。 Look at the request headers and the general.查看请求头和一般。 Make sure the request URL / backend has your backend server URL and the orgin / frontend has your frontend URL.确保请求 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. 4) 你的问题是你的后端没有正确连接你的前端,所以不要浪费你的时间去寻找其他错误。 Focus on debuging the http.put() method and the cors module and middleware you have imported.重点调试您导入的 http.put() 方法和 cors 模块和中间件。

Given the fact it is really a CORS issue - browsers 'preflight' the request using OPTIONS method.鉴于这确实是一个 CORS 问题 - 浏览器使用 OPTIONS 方法“预检”请求。 After the OPTIONS request succeeds the actual request (in your case PUT) is made.在 OPTIONS 请求成功后,实际请求(在您的情况下是 PUT)被发出。

Make sure, the backend responds to OPTION requests.确保后端响应 OPTION 请求。 You could easily catch all OPTION requests and return 200 OK or 204 NO CONTENT .您可以轻松捕获所有 OPTION 请求并返回200 OK204 NO CONTENT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM