nodejs express, ajax posting w/ jquery and receiving response

前端 未结 3 1398
轻奢々
轻奢々 2020-12-23 10:34

Having some trouble getting express to respond properly to my jquery ajax request. The actual posting is working fine, but no matter what I try I cant seem to actually get a

3条回答
  •  遥遥无期
    2020-12-23 10:58

    You are not sending a valid JSON response but a String containing the word json therefore the JSON.parse() is failing on the client side. Try this:

    app.post('/save', function(req, res) {
      console.log(req.body.objectData);
      res.contentType('json');
      res.send({ some: JSON.stringify({response:'json'}) });
    });
    

    JavaScript Object Notation is a way to share data between applications in object format. However, you cannot send an object over an HTTP request without first turning it into a string and sending it as a single variable. The functions JSON.parse() and JSON.stringify() do this for us.

提交回复
热议问题