简体   繁体   中英

Sails js http post request with content-type: application/json

I would like to understand how to write sails-js controller when http request looks as below, especially here I am trying to send raw-data with Content-Type: application/json . Please advise. (I am able to work with POST with form-data, In controller if read req.body I get form-data as key-value pairs of json)

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://localhost:1337/person/create",
  "method": "POST",
  "headers": {
    "content-type": "application/json"
  },
  "data": "{'companyID':'DEV','firstName':'Radha','lastName':'A',  'otherIDs': [123,345] }"
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

Try replacing the data line with this:

data: '{ "companyID":"TGDEV","firstName":"Naga","lastName":"A", "otherIDs": [123,345] }'

I think it has something to do with the JSON being invalid. Single quotes are not valid JSON. Check your JSON here if you're unsure, it's a good resource: http://jsonlint.com/

将数据行替换为: data: '{ "companyID":"TGDEV","firstName":"Naga","lastName":"A", "otherIDs": [123,345] } '

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM