[英]Getting Bad request for POST request using Axios and CORS in Django and react app
我在 postman 上向這個 url http://localhost:8000/api/auth/users/ 發出了一個post請求。 它成功了。 但是,在我的 React 前端嘗試同樣的事情會產生 400(錯誤的請求錯誤)。 它還說 Uncaught (in promise) Error: Request failed with status code 400
這是我的代碼 AuthAPi.js:
import API from "./API";
const config = {
headers :{
"Content-Type": "application/json",
}
}
const signIn = (email, password) => API.post("/auth/users/", {
params : {
email : email,
password : password
},
},
config
)
.Catch((error) => console.log( error.message ) );
export { signIn };
和 API.js
import axios from 'axios';
export default axios.create({
baseURL: `http://127.0.0.1:8000/api`,
headers: {
"Content-type": "application/json"
}
});
我已經嘗試了幾個沒有解決方案的鏈接。 我很樂意在這里得到幫助。
下面是控制台日志、網絡日志和 Django 服務器日志的屏幕截圖:
使用此代碼:
API.post("/auth/users/", {
params : {
email : email,
password : password
}
}
您將一個包含對象的對象作為發布數據傳遞給 axios。 Axios 不介意,但您的服務器不一樣。 相反,請嘗試:
API.post("/auth/users/", {
email : email,
password : password
}, config)
這應該有效。 如果要作為單個對象傳入,則需要在 api 調用之外聲明它:
let postData = {
email: email,
password: password
}
API.post("/auth/users/", postData, config)
params
是與請求一起發送的 URL 參數
你使用的post
實例方法的簽名是axios#post(url[, data[, config]])
,因此你應該像這樣提出你的請求
const signIn = (email, password) => API.post("/auth/users/", {
email : email,
password : password
},
config
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.