简体   繁体   English

尝试向 Django REST 服务器发出请求时获取 net::ERR_CONNECTION_REFUSED

[英]Getting net::ERR_CONNECTION_REFUSED when trying to make a request to Django REST Server

I am trying to test a Django app by using other devices in my local network.我正在尝试通过使用本地网络中的其他设备来测试 Django 应用程序。 I am using Angular as my client.我使用 Angular 作为我的客户。
So far, I am able to run my Angular app on other devices.到目前为止,我可以在其他设备上运行我的 Angular 应用程序。 However, whenever I try to make any sort of request, I get back a response specifying: net::ERR_CONNECTION_REFUSED但是,每当我尝试发出任何类型的请求时,我都会收到一个响应,指定: net::ERR_CONNECTION_REFUSED

I am able to properly run both the client app and Django REST server in my local machine but it's not the case when it comes to other devices.我能够在我的本地计算机上正确运行客户端应用程序和 Django REST 服务器,但在其他设备上并非如此。

Based on some of the already posted solutions, I have done:基于一些已经发布的解决方案,我已经完成了:

  1. Binding Django server to IP: 0.0.0.0:8000 by running python.\manage.py runsslserver --certificate.\ssl\server.crt --key.\ssl\server.key 0.0.0.0:8000 (I am using SSL for both client and server). Binding Django server to IP: 0.0.0.0:8000 by running python.\manage.py runsslserver --certificate.\ssl\server.crt --key.\ssl\server.key 0.0.0.0:8000 (I am using SSL对于客户端和服务器)。
  2. Setting ALLOWED_HOSTS to ['*'] in settings.py.在 settings.py 中将ALLOWED_HOSTS设置为['*']

But the error is still persistent.但错误仍然存在。 All the devices (including my local machine) which I am using is Windows if that's important.如果这很重要,我正在使用的所有设备(包括我的本地机器)都是 Windows。


Can someone help me in fixing this issue? 有人可以帮我解决这个问题吗? Or are there any other way to run the API server? 或者还有其他方法可以运行 API 服务器吗?

Hi i think this is a problem with cors.嗨,我认为这是 cors 的问题。 You can try installing django-cors-headers.您可以尝试安装 django-cors-headers。 Here is a small guide on how to do that. 是一个关于如何做到这一点的小指南。 I hope it helps you.我希望它对你有帮助。 Remember to add in CORS_ALLOWED_ORIGINS the host and port on which angular is running.请记住在 CORS_ALLOWED_ORIGINS 中添加运行 angular 的主机和端口。 For example if angular is running on port 3000:例如,如果 angular 在端口 3000 上运行:

CORS_ALLOWED_ORIGINS = [
    'http://localhost:3000',
]

Got it working.得到它的工作。
It was just a small bug in my angular app configuration for accessing API URL.这只是我的 angular 应用程序配置中的一个小错误,用于访问 API URL。
Just had to update the IP address (or the URI) in which the API server was running.只需更新运行 API 服务器的 IP 地址(或 URI)。

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

相关问题 Django:ERR_CONNECTION_REFUSED - Django: ERR_CONNECTION_REFUSED 连接到服务器上运行的 virtualenv/django 时如何解决 ERR_CONNECTION_REFUSED - How to resolve ERR_CONNECTION_REFUSED when connecting to virtualenv/django running on a server 错误选项 net::ERR_CONNECTION_REFUSED - Error OPTIONS net::ERR_CONNECTION_REFUSED 在 Gitlab CI 中为 Django 项目运行 Selenium 测试:ERR_CONNECTION_REFUSED on get live_server_url - Run Selenium tests for Django project in Gitlab CI : ERR_CONNECTION_REFUSED on get live_server_url 使用Heroku问题ERR_CONNECTION_REFUSED部署django - Deploy django with Heroku issue, ERR_CONNECTION_REFUSED 无法获取“图像”.net::ERR_CONNECTION_REFUSED (Gitpod) - Cannot get "images" net::ERR_CONNECTION_REFUSED (Gitpod) jQuery,Ajax(失败)净:: ERR_CONNECTION_REFUSED - Jquery, Ajax (failed) net::ERR_CONNECTION_REFUSED 使用 Eclipse Pydev 打开 dtale 时浏览器上的 ERR_CONNECTION_REFUSED - ERR_CONNECTION_REFUSED on browser when opening dtale with Eclipse Pydev 在本地上测试Flask应用程序时出现ERR_CONNECTION_REFUSED - ERR_CONNECTION_REFUSED when testing flask app on localhost 带有 Selenium 的 StaticLiveServerTestCase:ERR_CONNECTION_REFUSED - StaticLiveServerTestCase with Selenium: ERR_CONNECTION_REFUSED
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM