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

使用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.

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站