javascript 正文数据未在 axios 请求中发送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52561124/
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:58:49  来源:igfitidea点击:

body data not sent in axios request

javascriptnode.jsreactjsaxios

提问by Azima

I am trying to send data through axios request to my backend script, but the body looks empty.

我正在尝试通过 axios 请求将数据发送到我的后端脚本,但正文看起来是空的。

Here's a request sent from front-end:

这是从前端发送的请求:

axios.request({
  method: 'GET',
  url: `http://localhost:4444/next/api`,
  headers: {
    'Authorization': token
  },
  data: {
    next_swastik: 'lets add something here'
  },

}).then((res)=>{  
  console.log("api call sucessfull",res);

}).catch((err)=>{
  console.log("api call unsucessfull",err);

  this.props.toggleLoading(false);
})

Here's a back-end:

这是一个后端:

app.get('/next/api', verifyToken, function(req, res) {
console.log(req.body);

})

})

But I am getting {}empty body. I am getting headers and other data but not data.

但我的{}身体越来越空虚。我正在获取标题和其他数据,但不是数据。

采纳答案by pidizzle

GET requests should not have a body.

GET 请求不应有正文。

Change the method from 'GET' to 'POST'

将方法从“GET”更改为“POST”

Like so:

像这样:

axios.request({
  method: 'POST',
  url: `http://localhost:4444/next/api`,
  headers: {
    'Authorization': token
  },
  data: {
    next_swastik: 'lets add something here'
  },

})

and change your api to expect a post

并更改您的 api 以期待发布

app.post('/next/api', verifyToken, function(req, res) {
console.log(req.body);
});

or

或者

Change the dataproperty to params

data属性更改为params

axios.request({
  method: 'GET',
  url: `http://localhost:4444/next/api`,
  headers: {
    'Authorization': token
  },
  params: {
    next_swastik: 'lets add something here'
  },

})

and change the api to log out the params

并更改api以注销参数

app.get('/next/api', verifyToken, function(req, res) {
console.log(req.params);
});

and like @MaieonBrix said, make sure that your headers contain the content type that you are sending.

就像@MaieonBrix 所说的那样,确保您的标头包含您要发送的内容类型。

回答by MaieonBrix

It looks like you only have two points left to make it work :

看起来你只剩下两点才能让它工作:

  • one : the http method should be set to POSTinstead of GETsince you want to send something.

  • two : you can then add the http header (like what you did with the authorization header) Content-Type: 'application/json`

  • 一:http 方法应该设置为POST而不是GET因为您想发送一些东西。

  • 二:然后您可以添加 http 标头(就像您对授权标头所做的一样)Content-Type:'application/json`

On the back-end don't forget to use some kind of body parser utility package like this one : body-parserand set it up with your app.

在后端,不要忘记使用某种身体解析器实用程序包,例如:body-parser并使用您的应用程序进行设置。

I suppose your server is using express, here is how you will do it with express :

我想您的服务器正在使用 express,以下是您将如何使用 express:

const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const jsonParser = bodyParser.json();

app.use(jsonParser); // use it globally
app.get('your_route', jsonParser, otherMiddleware, (req, res) => ...); // use it for specific routes

/* ... rest of your code */

回答by Pablo Papalardo

Try this

试试这个

this.axios('realties', { params: this.filter })