React native 当我在本地主机上使用django rest framework api时,为什么在操作中出现给定cors错误

React native 当我在本地主机上使用django rest framework api时,为什么在操作中出现给定cors错误,react-native,django-rest-framework,axios,localhost,http-status-code-400,React Native,Django Rest Framework,Axios,Localhost,Http Status Code 400,我正在使用带有api的react本机应用程序。由django rest框架编写的API。我在localhost:8000中运行api。当我尝试用axios连接api时,我遇到了cors问题。我添加了django cors的头球,但也没用。有人能解决吗?谢谢 Frontend/actions.js export const loginUser = (username, password)=>dispatch=> { const body =JSON.stringify({ userna

我正在使用带有api的react本机应用程序。由django rest框架编写的API。我在localhost:8000中运行api。当我尝试用axios连接api时,我遇到了cors问题。我添加了django cors的头球,但也没用。有人能解决吗?谢谢

Frontend/actions.js

export const loginUser = (username, password)=>dispatch=> {
const body =JSON.stringify({ username, password })
// headers
const config ={
    headers:{
        'Accept': 'text/html',
        'Content-Type': 'application/x-www-form-urlencoded',
        // "Access-Control-Allow-Origin": '*'
    }
}
const root =API()

    console.log(body);
    axios.post("http://localhost:8000/api/login/", body, config)
        .then(res =>{
            dispatch({
                type:$AT.LOGIN_USER,
                payload: res.data
            })
        }).catch(err=>{
            console.log(err);
        })
Login.js

const onLoginPressed =() =>{
    const usernameError = nameValidator(username.username);
    const passwordError = passwordValidator(password.password);

    if (usernameError || passwordError) {
        setUsername({ ...username, error: usernameError });
        setPassword({ ...password, error: passwordError });
    }
    else{
        // console.log("GIRIS YAPILIYOR");
        dispatch(loginUser(username.username, password.password));
    }

}
API/settings.py

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

# my apps
'account',
# third party app
'corsheaders',
'knox',
'rest_framework',
'drf_yasg',
]

 MIDDLEWARE = [
 ....
'corsheaders.middleware.CorsMiddleware',
 ....
]
我在chorme上给出了这个错误:

在网络上:

您收到一个CORS错误,因为服务器在添加CORS头之前发出了一个
400错误请求


不清楚为什么它会抛出错误的请求,因为您没有提供任何关于该请求的调试信息,但是,当主体是JSON而不是表单编码的数据时,它似乎与您声称的“内容类型”:“application/x-www-form-urlencoded”
有关。

如果您想使用axios,请使用IP地址进行配置,您可以检查ifconfig并运行django python manager.py runserver IP:8000

尝试添加CORS_ORIGIN_REGEX_WHITELIST+=[“”]我试过了,但没有工作。您可以发布此400错误的错误响应吗?