【问题标题】:Why does ajax post encoded data instead of JSON为什么ajax发布编码数据而不是JSON
【发布时间】:2019-08-17 16:20:29
【问题描述】:

我有一个 ajax POST,它对数据进行编码,而不是以 JSON 格式发送。我必须遗漏一些东西,但我无法缩小范围。该 API 在 Insomnia 中有效,但不适用于代码。

我将 content-Type 设置为 application/json 并且我尝试将 contentType 设置为 false 并明确指定标头。无论哪种方式,主机都在接收应用程序/json。我尝试过以字符串、对象和函数的形式发送。

data: jsonPacket,
data: JSON.stringify(jsonPacket),
data:function () {
   return "foo bar";
}

所有尝试都会产生编码文本。

构建 json 对象

          JSONpacket = {
            "Destination": {
              "ToAddresses": [
                $('email').val()
              ],
                "BccAddresses": [
                  "sombody@somewhere.com"
                ]
            },
            "Message": {
              "Body": {
                "Text": {
                  "Data" : "Test email",
                }
              },
              "Subject": {
                "Data": "Message received from Website"
              }
            },
            "Source": "nobody@nowhere.com"
          };

拨打电话

          $.ajax({
            type: "POST",
            url: "https://x9beos0tm4.execute-api.us-west-2.amazonaws.com/stage/<sanitized>",
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            headers: {
              "x-Api-Key": '**************************'
            },
            data: JSONstringify(JSONpacket),
            success: function (data, status) {
              $('#email_response').html(data);
              $('#email_response').addClass('alert alert-success');
              $('#email_response').slideDown();
              setTimeout(function (e) {
                $('#email_response').slideUp();
                $('#email_response').val('');
                $('#email_response').removeClass('alert alert-success');
                $('#email').val('');
                $('#name').val('');
                $('#phone').val('');
                $('#message').text('');
              }, 15000);
            },
            error: function (xhr, status, errMessage) {
              if (errMessage == "")
                errMessage = xhr.statusText + " : " + xhr.status + ' - ' + xhr.responseJSON.message;
              $('#email_response').addClass('alert alert-danger');
              $('#email_response').html(errMessage);
              $('#email_response').slideDown();
              setTimeout(function (e) {
                $('#email_response').slideUp();
                $('#email_response').html('');
                $('#email_response').removeClass('alert alert-danger');
              }, 15000);
            }
          });

从 ajax 调用时收到的数据

"Destination%5BToAddresses%5D%5B%5D=sombodey%40somewhere.com&Destination%5BBccAddresses%5D%5B%5D=someonelse%40somewhere.com&Destination%5BBccAddresses%5D%5B%5D=nobody%40nowhere.com&Message%5BBody%5D%5BText%5D%5BData%5D=****+****+contact+Email%0D%0ASent+on+Saturday+August8+17th+2019+%40+10%3A04+am%0D%0A%0D%0AName%3A+******+*********%0D%0AEmail%3A+sombody%40somewhere.com%0D%0APhome%3A+9999999999%0D%0AMessage%3A%0D%0Asdffg%0D%0A&Message%5BSubject%5D%5BData%5D=Message+received+from+Website&Source=nobody%40nowhere.com

Insomnia 调用时收到的数据(如邮递员)

"body": "{\n  \"Destination\": {\n    \"ToAddresses\": [\n      \"somebody@somewhere.com\",\n        \"somebodyelse@somewhere.com\"    ]\n  },\n  \"Message\": {\n    \"Body\": {\n      \"Text\": {\n        \"Data\": \"Test email body from API\"\n      }\n    },\n    \"Subject\": {\n      \"Data\": \"Different Test Email Subject FROM API\"\n    }\n  },\n  \"Source\": \"webadmin@nowhere.com\"\n}"

【问题讨论】:

  • 你的JSONstringify()函数是做什么的?
  • 注意$('email').val() , email 不是 HTML 标签。
  • 在这里发错字 - s/b #email

标签: jquery json ajax aws-lambda


【解决方案1】:

已修复

愚蠢的人类把戏

两个问题:

1.) its JSON.stringify() not JSONstringify()
2.) malformed json:  [$('email').val()] should be $('email').val()

【讨论】:

  • 注意$('email').val() , email 不是 HTML 标签。
【解决方案2】:

\n 字符是在 JSON.stringify 中转换后的换行符。如果您对其执行 JSON.parse,您将获得没有那些 \n 字符的原始对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    相关资源
    最近更新 更多