【发布时间】:2018-11-29 16:37:44
【问题描述】:
我正在尝试通过 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 请求会失败?
【问题讨论】:
-
我遇到了同样的问题。我的后端代码中有语法错误。通过邮递员检查您的请求并确保您的服务器端代码运行正确。
标签: javascript post flask axios internal-server-error