【问题标题】:Error: Request failed with status code 400. missing headers? error in backend?错误:请求失败,状态码为 400。缺少标头?后端出错?
【发布时间】:2018-01-26 00:41:50
【问题描述】:

我在 React Native(android 模拟器)中使用 axios 向我的 localhost(本例中为 10.0.0.2:8000)发送 POST 请求,我从 Django RESTful Framework 后端收到 400 错误。

这是我的动作创建者

export const doAuthLogin = ({ username, password }) => dispatch => {
  axios.post(`${ROOT_URL}/rest-auth/login/`, {
    username,
    password
  }).then(response => {
    console.log(response);
    // Save Token post is Already await.
    AsyncStorage.setItem('auth_token', response.token);
    dispatch({ type: AUTH_LOGIN_SUCCESS, payload: response.token });
  })
  .catch(response => {
    console.log(response);
    dispatch({ type: AUTH_LOGIN_FAIL, payload: response.non_field_errors });
  });
};

这是来自远程调试器 JS 的错误消息。它只是来自 axios.post 的 catch 的 console.log(response)。

Error: Request failed with status code 400
    at createError (createError.js:16)
    at settle (settle.js:18)
    at XMLHttpRequest.handleLoad (xhr.js:77)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:538)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:381)
    at XMLHttpRequest.js:485
    at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
    at MessageQueue.__callFunction (MessageQueue.js:260)
    at MessageQueue.js:101

控制台中的错误消息, [17/Aug/2017 14:14:10]“POST /rest-auth/login/HTTP/1.1”400 40

这是settings.py的一部分,

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

INSTALLED_APPS = [
    'corsheaders',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'allauth',
    'allauth.account',
    'rest_auth.registration',
    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',
    'profiles',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',
    'allauth.socialaccount.providers.twitter',
    'django.contrib.sites',
]

SITE_ID = 1

CORS_ORIGIN_WHITELIST = (
    '10.0.2.2',
    'localhost'
)

我想我可能在动作创建器中有错误。缺少标题或...

这个错误没有给我任何超过 400 的提示,所以我完全迷路了。你能帮我解决这个问题吗?

谢谢

【问题讨论】:

  • 您期望 djagno 中的哪些参数?可能先尝试使用邮递员之类的工具发送请求,然后再使用代码尝试
  • 响应正文是什么?
  • 它在 Postman 中工作(localhost:8000/rest-auth/login 使用用户名和密码)
  • 响应正文只是用户名和密码。用户名可以是电子邮件或用户名。
  • 项目中允许的主机设置

标签: django reactjs react-native axios


【解决方案1】:

感谢大家的评论。 问题是我实际上是在发送未定义的用户名和密码。

{username, password } -> (username, password)

这个问题是非常愚蠢的问题,这个代码是完美的。没有 CORS 错误、正确的响应正文和正确的允许主机。实际上,您的 cmets 使我确信我的 React 代码可能有问题。我在每一行中对日志变量进行了控制台。

非常感谢!现在我可以睡觉了

【讨论】:

  • this.props.doAuthLogin(this.state.username, this.state.password)
猜你喜欢
  • 2020-10-21
  • 1970-01-01
  • 2021-10-03
  • 2021-01-12
  • 2021-09-15
  • 1970-01-01
  • 2020-04-25
  • 2013-07-21
  • 1970-01-01
相关资源
最近更新 更多