POST node in the body has been more than an empty object, which leads to less demand value

In the process of learning VUE's own simple to learn a bit back-end node, write a simple back-end server, the front and rear cross-domain, but learning to post a request axios, has been the back-end get less than value, do not know why, after each request one more empty body objects

app.use('/postcomment/', function (req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With,Origin,Content-Type,Accept");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    
    var content=req
    console.log(content)
    var user_name=req.body.artid
    console.log(user_name)

    

    fs.readFile('./comments1.json', function (err, data) {
        if (err) {
            console.error(err);
            return;
        }
        var person = data.toString();
        person = JSON.parse(person);
        res.send(person)
        res.end();
    });
})

VUE front end of axios

postComment() {
      // 校验是否为空
  if(this.msg.trim().length===0){
    return Toast('评论内容不能为空!');
  }

      this.$ajax({
        method: "post",
        url: "/postcomment/",
        data: {
          artid: this.id,
          content: this.msg.trim()
        }
      }).then(response=>{
        console.log(response.data);
        if(response.data.status===0){
          var cmt={user_name:'匿名用户',add_time:Date.now(),content:this.msg.trim()
          }
          this.comments.unshift(cmt);
          this.msg=""
        }
      }).catch(error => {
          console.log(error);
          Toast({
            message: "增加评论失败..."
          });
        });
    }

Finally, check a very long time, much of this is found in the body req object

[Symbol(outHeadersKey)]:
      [Object: null prototype] {
        'x-powered-by': [Array],
        'access-control-allow-origin': [Array],
        'access-control-allow-headers': [Array],
        'access-control-allow-methods': [Array] } },
  body: {} }

I think of a reason not to increase cross-domain header

app.use('/postcomment/', function (req, res) {
    
    var content=req
    console.log(content)
    var user_name=req.body.artid
    console.log(user_name)

    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With,Origin,Content-Type,Accept");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')

    

    fs.readFile('./comments1.json', function (err, data) {
        if (err) {
            console.error(err);
            return;
        }
        var person = data.toString();
        person = JSON.parse(person);
        res.send(person)
        res.end();
    });
})

Then end up with this test, resolve,

While seemingly a simple question, but for me this novice, node Little is really bothering quite a while

Guess you like

Origin blog.csdn.net/an501920078/article/details/92117574