当相同的数据和URL与邮递员一起工作时,Axios Post请求返回错误400(Django DRF React.js)
我正在创建一个应用程序,它使用Django作为后端,React.js作为前端。我将DRF用于从React.js到Django的api调用,但在发送post请求时遇到了一个问题。当我在React.js中使用axios发送包含JSON格式相关数据的post请求时,它返回的错误代码为400。然而,当我复制相同的JSON数据并使用它在Postman中发送post请求时,没有出现错误,代码是201(已创建)。我不太清楚为什么会出现这个问题,我已经在Django settings.py中设置了必要的设置,以确保授予权限(出于测试目的,我只是将其设置为“允许所有”)。我设法打印出失败的post请求背后的错误消息,但我并不真正理解错误消息的含义。如果有任何其他方式打印出更多的错误消息,让我知道从哪里开始,请让我知道,谢谢!感谢您的帮助当相同的数据和URL与邮递员一起工作时,Axios Post请求返回错误400(Django DRF React.js),django,reactjs,django-rest-framework,axios,Django,Reactjs,Django Rest Framework,Axios,我正在创建一个应用程序,它使用Django作为后端,React.js作为前端。我将DRF用于从React.js到Django的api调用,但在发送post请求时遇到了一个问题。当我在React.js中使用axios发送包含JSON格式相关数据的post请求时,它返回的错误代码为400。然而,当我复制相同的JSON数据并使用它在Postman中发送post请求时,没有出现错误,代码是201(已创建)。我不太清楚为什么会出现这个问题,我已经在Django settings.py中设置了必要的设置,以
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny',
]
}
CORS_ORIGIN_ALLOW_ALL = True
控制台中400错误代码之后的错误消息
{message: "Request failed with status code 400", name: "Error", description: undefined, number:
undefined, fileName: undefined, …}
message: "Request failed with status code 400"
name: "Error"
description: undefined
number: undefined
fileName: undefined
lineNumber: undefined
columnNumber: undefined
stack: "Error: Request failed with status code 400↵ at createError
(http://localhost:3000/static/js/0.chunk.js:117160:15)↵ at settle
(http://localhost:3000/static/js/0.chunk.js:117381:12)↵ at XMLHttpRequest.handleLoad
(http://localhost:3000/static/js/0.chunk.js:116635:7)"
config:
url: "http://localhost:8000/api/customer-information/"
method: "post"
data: "{"customer_name":"test",
"telephone_number":"123","incorporation_country":"A","address":"B",
"fax_number":"123","control":"d"}"
headers:
Accept: "application/json, text/plain, */*"
Content-Type: "application/x-www-form-urlencoded"
__proto__: Object
transformRequest: [ƒ]
transformResponse: [ƒ]
timeout: 0
adapter: ƒ xhrAdapter(config)
xsrfCookieName: "XSRF-TOKEN"
xsrfHeaderName: "X-XSRF-TOKEN"
maxContentLength: -1
validateStatus: ƒ validateStatus(status)
__proto__: Object
code: undefined
__proto__: Object
axios请求
onSubmit = (data) => {
data = JSON.stringify(data);
console.log(data);
axios.post('http://localhost:8000/api/customer-information/', data)
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error.toJSON());
})
};