[英]Axios POST request fails with error status code 500: Internal Server error
我正在尝试通过 Axios 在本地发送一个带有用户名和密码的 POST 请求。
我在http://127.0.0.1:5000/login上部署一个 Flask 应用程序,它处理 /login 路由。 POST 请求失败,出现以下错误
POST http://127.0.0.1:5000/login 500 (INTERNAL SERVER ERROR)
Error: Request failed with status code 500
at createError (createError.js:16)
at settle (settle.js:18)
at XMLHttpRequest.handleLoad (xhr.js:77)
我研究了一下,认为这可能是 CORS 的问题,但情况似乎并非如此,因为我尝试了 Axios GET 请求并且它工作正常(响应记录正确)。 这是我的代码的一部分
axios.get("http://127.0.0.1:5000").then(function(response) {
console.log(response);
}).catch(function(error) {
console.log(error);
})
axios.post("http://127.0.0.1:5000/login", {
username: this.state.username,
password: this.state.password
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.log(error);
})
查看 Chrome DevTools,我可以看到 POST 请求负载已正确填充。 然后我尝试使用以下代码在 Flask 应用程序中打印出服务器端的密钥,但我什么也没得到,是空的。 (这是预期的,因为 POST 请求失败)
dict = request.form
for key in dict:
print('form key '+dict[key])
但是,将 Postman 与相应的键和值一起使用可以正常工作并返回响应并打印出键(见上文)。 失败从何而来? 为什么 GET 看起来工作正常时 POST 请求会失败?
2021 年 2 月。在这上面浪费了 2 个小时。 对这个著名的互联网图书馆帮助不大。
解决方案:
error
总是500 internal server error
error.response.data
而不是error
。代码:
try {
let result = await axios.post( // any call like get
"http://localhost:3001/user", // your URL
{ // data if post, put
some: "data",
}
);
console.log(result.response.data);
} catch (error) {
console.error(error.response.data); // NOTE - use "error.response.data` (not "error")
}
更新:
我最终编写了一个用于处理错误的通用函数:
文件:common.app.js
export const errorUtils = {
getError: (error) => {
let e = error;
if (error.response) {
e = error.response.data; // data, status, headers
if (error.response.data && error.response.data.error) {
e = error.response.data.error; // my app specific keys override
}
} else if (error.message) {
e = error.message;
} else {
e = "Unknown error occured";
}
return e;
},
};
所以我也陷入了同样的问题,我发现的解决方案是这样的:
let data = JSON.stringify({
username: this.state.username,
password: password
});
const response = axios.post(url,data,{headers:{"Content-Type" : "application/json"}});
这个解决方案对我有用。
显然 Axios 对原始 JSON 对象并不友好
{username: this.state.username, password: password}
但是将数据传递到 FormData 对象似乎工作得很好!
大多数情况下,这是因为使用了错误的内容类型 header 。
打开邮递员并查看“正文”选项卡。 在那里您可以找到帖子数据的内容类型。 它也可以从“标题”选项卡访问。 应该有一个Content-Type
标头。 您通过 POST 请求发送的正确data
格式取决于Content-Type
标头。 例如,json 内容类型需要一个 json(javascript 对象)数据或 form-data 内容类型需要一个 FormData。
要在 axios 中设置标题,请像这样更改代码:
axios.post("http://127.0.0.1:5000/login", {
username: this.state.username,
password: this.state.password
}, {
headers: {'Content-Type': 'application/json'}
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.log(error);
})
工作了两个小时后,我意识到我在身体和数据上犯了一个错误。 所以,在 axios 中确保你像这样传递数据。
async function loadToken(){
try{
response = await axios({
url: ``,
headers: {
'Authorization': '',
'Content-Type': '',
},
data: '',
method: 'POST'
});
let data = response.data;
return {
tokenInfo:data,
timestamp:new Date().getTime()
}
} catch(err) {
console.log("err->", err.response.data)
return res.status(500).send({ret_code: ReturnCodes.SOMETHING_WENT_WRONG});
}
}
我之前的代码是这样传数据的,这是错误的
async function refreshToken(){
try{
let headers = {
authorization: '',
'Content-Type': ''
}
let url = ``
let body = {
grant_type: '',
refresh_token: global.tokenInfo.refresh_token
}
data = await axios.post(url, body, {headers});
let data = response.data
console.log(data)
return {
tokenInfo:data,
timestamp:new Date().getTime()
}
} catch(err) {
console.log("err->", err.response)
return res.status(500).send({ret_code: ReturnCodes.SOMETHING_WENT_WRONG});
}
}
只需尝试我的第一个代码,希望能解决您的问题。
我有类似的错误我有 JSON 资本,它应该是小写的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.