【问题标题】:Cannot connect to django rest api with react native network error无法通过反应本机网络错误连接到 django rest api
【发布时间】:2020-12-04 04:25:24
【问题描述】:

我是 Django 和 React Native 的新手。 我尝试制作一个使用 axios 将数据发送到 django 后端的应用程序,但出现网络错误。 我尝试在 192.168.xxx 中更改我的 localhost ip,但没有任何改变。

所以我不明白为什么我没有任何请求。

这是我的代码:

姜戈:

CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (
    'http://localhost:8081',
    'http://localhost:19006',
    'http://localhost:19002',
    'http://192.168.101.6:19000',
)

Axios:

import axios from "axios";
    
export default axios.create({
  baseURL: "http://192.168.101.6:8080/api",
  headers: {
    "Content-type": "application/json"
  }
});

请求:

import http from "../http-common";
    
const getAll = () => {
  return http.get("/tutorials");
};
    
const get = id => {
  return http.get(`/tutorials/${id}`);
};
    
const create = data => {
  return http.post("/tutorials", data);
};

【问题讨论】:

  • 你添加'corsheaders.middleware.CorsMiddleware'到你的Django MIDDLEWARE了吗?

标签: django react-native django-rest-framework


【解决方案1】:

欢迎@marcus95!

我认为你写错了:

CORS_ORIGIN_WHITELIST = (
    ...
    'http://192.168.101.6:19000',
     http://192.168.101.6:8080,
)

http://192.168.101.6:8080 与您的 axios 配置相同,放入您的 CORS_ORIGIN_WHITELIST。

【讨论】:

    猜你喜欢
    • 2019-07-02
    • 1970-01-01
    • 2021-11-11
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 2019-08-05
    • 1970-01-01
    相关资源
    最近更新 更多