简体   繁体   English

使用jQuery / Javascript解析Mandrill JSON响应

[英]Parsing Mandrill JSON Response with jQuery / Javascript

I have a php script which is sucessfully connecting to and sending an email through the Mandrill API. 我有一个PHP脚本,它已成功连接到Mandrill API并通过其发送电子邮件。 As dumb as it sounds, I can't figure out how to parse the following JSON reponse: 听起来很愚蠢,但我不知道如何解析以下JSON响应:

Array
(
    [0] => Array
        (
            [email] => matt@mattblum.com
            [status] => sent
            [_id] => eedced1b58e24668907024e937afeabd
            [reject_reason] => 
        )

)

Full ajax call is: 完整的ajax调用是:

$.ajax({
    type: 'POST',
    url: 'mandril.php',
    data: formData.serialize(),
    success: function(returnedData) {
        var response = returnedData;
        status = response[0]['status']; // I've also tried different combos of this

        if(status == "sent") {
            msgs('Message sent!');
            var alreadySent = true;
        } else {
            msgs(response);
        }

      },
      error: function(e) {
          console.log(e);
      }

The code I've tied to read the 'status' element: 我绑定来读取“ status”元素的代码:

console.log(response[0]['status']);
console.log(response[0][1]);
console.log(response[0][0]['status']);
console.log(response[0][0][1]);

Another thing I don't understand is that: 我不明白的另一件事是:

var response = returnedData;
console.log(response[0]);

Returns a capital 'A' and nothing else. 返回大写字母“ A”,而不返回其他任何内容。

I'm sure it's something dumb that I'm doing but any help is greatly appreciated. 我敢肯定,我在做些愚蠢的事情,但对我们的帮助将不胜感激。

Matt, Looks like your PHP array syntax is being captured as text and sent as a response to your post. 马特,看来您的PHP数组语法已被捕获为文本并作为对您帖子的响应发送。 If you specify JSON on the Mandrill/PHP side (and the ajax/jquery side) your jquery call should be able to parse the response. 如果您在Mandrill / PHP端(和ajax / jquery端)指定JSON,则您的jquery调用应该能够解析响应。

console.log(response[0]) is returning 'A' because that's the first character of the string. console.log(response [0])返回“ A”,因为这是字符串的第一个字符。

Add 'json' on the query side and json_encode($response) on the php side. 在查询侧添加“ json”,在php侧添加json_encode($ response)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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