简体   繁体   English

如何使用 React-native 访问 Django 本地服务器数据?

[英]How to access Django local server data with React-native?

I build a Django API using django rest framework.我使用 django rest 框架构建了 Django API。 And a want to access the json response data of the api in my react-native app.并且想在我的 react-native 应用程序中访问 api 的 json 响应数据。 But a get an error ( Network request failed )但是出现错误(网络请求失败

DJANGO RESPONSE DJANGO 响应在此处输入图像描述

REACT-NATIVE REACT-NATIVE 在此处输入图像描述

ERROR错误

Network request failed网络请求失败

  • node_modules/whatwg-fetch/dist/fetch.umd.js:505:17 in setTimeout$argument_0 setTimeout$argument_0 中的 node_modules/whatwg-fetch/dist/fetch.umd.js:505:17
  • node_modules/react-native/Libraries/Core/Timers/JSTimers.js:135:14 in _callTimer _callTimer 中的 node_modules/react-native/Libraries/Core/Timers/JSTimers.js:135:14
  • node_modules/react-native/Libraries/Core/Timers/JSTimers.js:387:16 in callTimers node_modules/react-native/Libraries/Core/Timers/JSTimers.js:387:16 in callTimers
  • node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:425:19 in __callFunction __callFunction 中的 node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:425:19
  • node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:112:6 in __guard$argument_0 __guard$argument_0 中的 node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:112:6
  • node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:373:10 in __guard __guard 中的 node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:373:10
  • node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:111:4 in callFunctionReturnFlushedQueue node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:111:4 in callFunctionReturnFlushedQueue
  • [native code]:null in callFunctionReturnFlushedQueue 【原生代码】:null in callFunctionReturnFlushedQueue

Please consider take the following suggestions:请考虑采取以下建议:

  1. activate developer mode on your device and than turn on the "USB Debugging" mode.在您的设备上激活开发者模式,然后打开“USB 调试”模式。

  2. Plug your device into a USB port on your pc将您的设备插入电脑上的 USB 端口

  3. Add this line into your package.json file inside the scripts object:将此行添加到scripts object 内的package.json文件中:

    "android-dev": "adb reverse tcp:8081 tcp:8081"

  4. Check if CORS is configured in your settings.py Django project file and make sure to allow all hosts to access the project (of specify the hosts if that's important to your tests).检查您的settings.py Django 项目文件中是否配置了 CORS 并确保允许所有主机访问该项目(如果这对您的测试很重要,请指定主机)。

    INSTALLED_APPS = [... 'rest_framework', 'corsheaders', ... ] INSTALLED_APPS = [... 'rest_framework', 'corsheaders', ... ]

     MIDDLEWARE = [... 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ... ] CORS_ORIGIN_ALLOW_ALL = True ALLOWED_HOSTS = ['*']
  5. Check your local ip (usually starts with 192.168... ).检查您当地的 ip(通常以192.168...开头)。 Do this by running ipconfig on windows or ifconfig on linux.通过在 windows 上运行ipconfig或在 linux 上运行ifconfig来执行此操作。

  6. Make sure you are running your Django project with the same IP as your local network.确保您使用与本地网络相同的 IP 运行 Django 项目。 For exemple, my local ip is 192.168.15.2, so I run python manage.py runserver 192.168.15.2:8000 .比如我本地的 ip 是 192.168.15.2,所以我运行python manage.py runserver 192.168.15.2:8000 In my case I like to use Pycharm, so I configure it in the server settings在我的情况下,我喜欢使用 Pycharm,所以我在服务器设置中配置它在此处输入图像描述

  7. And for the last step, use that same IP in your requests adding the "Access-Control-Allow-Origin":'*' in the request header.最后一步,在您的请求中使用相同的 IP,在请求 header 中添加"Access-Control-Allow-Origin":'*' So it would be something like this:所以它会是这样的:

在此处输入图像描述

Note that I use axios for sending requests, but you can check how add the headers in your request using fetch here .请注意,我使用 axios 来发送请求,但您可以在此处使用 fetch 检查如何在请求中添加标头。

I wish you the best.我希望你是最好的。 This is really a annoying problem that I took some time to figure it out.这确实是一个烦人的问题,我花了一些时间才弄清楚。

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

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