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.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用