weixin_33725126 2017-04-06 16:34 采纳率: 0%
浏览 204

使用Ajax的POST JsonArray

I have a JSON Array that I am trying to post to SENDGRID using Ajax. Using Postman I am able to post with no issues however when I post the data in my .js file I keep getting an error (bad request = missing parameters).

Any help is appreciated.

Note: The values are in fact valid. I have removed the identifying information for safety.

CHROME PAYLOAD:

enter image description here

AJAX Call:

var mailUrl = "https://api.sendgrid.com/v3/mail/send";
var postdata = '{"personalizations": [{"to":[{"to email"}],"from": {"email":"from email"},"subject":"Hello, World!" , "content" : [{ "type":"text/plain" , "value":"TestMessage!" }]}]}'
$.ajax({
    type: 'POST',
    headers: {Authorization: "Bearer APIKEY"},
    url: mailUrl,
    contentType: "application/json",
    data: JSON.stringify(postdata),
    success: function (res) {
         alert('ok');
    },
    error: function (res) {
         alert('problems');
    }
});
  • 写回答

2条回答 默认 最新

  • weixin_33690963 2017-04-06 17:09
    关注

    The string stored in the variable is a valid JSON. Calling JSON.stringify() on a JSON will escape all the special characters like " and that escaped string will not be deserialized to the object you intended.

    While a string is still a valid JSON according to some specifications, The specifications for application/json stated in RFC4627

    An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members).

    make the returned string invalid for post.

    Sending the string itself without serializing it again will likely work.

    评论

报告相同问题?