nodejs express, ajax posting w/ jquery and receiving response

前端 未结 3 1391
轻奢々
轻奢々 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 11:23

    Since you are using express,

    res.contentType('json');
    

    should be:

    res.type('json');
    

    but setting the type is not required since it is done automatically for you.

    See the express api docs on res.type.

    Also note that, for express, res.send({blah:"gee"}); automatically converts json objects using JSON.stringify internally. After clicking the above link, click on res.send and, while you are at it, res.json which saves a little processor overhead when you know you are sending JSON. Note that if you send JSON, the type is automatically set to JSON.

    Always best to look at the source! Note that res.send calls this.json when it detects JSON, and that res.json calls this.send (yeah seems like a loop but it all works out).

提交回复
热议问题