javascript Axios POST 请求失败,错误状态代码为 500:内部服务器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50950011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:21:02  来源:igfitidea点击:

Axios POST request fails with error status code 500: Internal Server error

javascriptpostflaskaxiosinternal-server-error

提问by M Xiao

I'm trying to send a POST request locally with a username and password in the body through Axios.

我正在尝试通过 Axios 在正文中使用用户名和密码在本地发送 POST 请求。

I'm deploying a Flask app on http://127.0.0.1:5000/login, which handles the /login route. The POST request fails with the following error

我正在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)

I researched a bit and thought it might be a problem with CORS, but this doesn't seem to be the case because I tried an Axios GET request and it worked fine (response logged properly). Here's part of my code

我研究了一下,认为这可能是 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);
      })

Looking at Chrome DevTools, I can see that the POST request payload is properly populated. I then tried printing out the keys server-side in the Flask app using the following code, but I got nothing, empty. (which was expected since the POST request failed)

查看 Chrome DevTools,我可以看到 POST 请求有效负载已正确填充。然后我尝试使用以下代码在 Flask 应用程序中打印出服务器端的密钥,但我什么也没得到,空的。(这是预期的,因为 POST 请求失败)

dict = request.form
    for key in dict:
        print('form key '+dict[key])

HOWEVER using Postman with the corresponding keys and values works properly and returns a response and prints out the keys (see above). Where is the failure coming from? Why would the POST request fail when a GET seems to work just fine?

但是,将 Postman 与相应的键和值一起使用可以正常工作并返回响应并打印出键(见上文)。失败从何而来?当 GET 似乎工作正常时,为什么 POST 请求会失败?

回答by M Xiao

Apparently Axios didn't take kindly to the raw JSON object

显然 Axios 对原始 JSON 对象并不友好

{username: this.state.username, password: password} 

but passing the data into a FormData object seemed to work just fine!

但是将数据传递到 FormData 对象似乎工作得很好!

回答by Aman Singh

So I also got stuck in the same problem and the solution that I found was something like this :

所以我也陷入了同样的问题,我发现的解决方案是这样的:

let data = JSON.stringify({
  username: this.state.username,
  password: password
});

const response = axios.post(url,data,{headers:{"Content-Type" : "application/json"}});

This solution worked for me.

这个解决方案对我有用。