2014-03-19 92 views
1

我有一個可以通過Mandrill API成功連接併發送電子郵件的php腳本。儘管聽起來很愚蠢,我無法弄清楚如何解析以下JSON的回覆:用jQuery/Javascript解析Mandrill JSON響應

Array 
(
    [0] => Array 
     (
      [email] => [email protected] 
      [status] => sent 
      [_id] => eedced1b58e24668907024e937afeabd 
      [reject_reason] => 
     ) 

) 

完全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); 
     } 

我綁的代碼讀取「狀態「元素:

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

另一件事我不明白的是:

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

返回大寫'A',沒有別的。

我敢肯定,這是愚蠢的,我正在做,但任何幫助,不勝感激。

+0

'success:function(responseData){console.log(responseData)}'return? – guest271314

+0

上面格式化的整個數組。 –

回答

0

Matt, 看起來您的PHP數組語法正在被捕獲爲文本並作爲您的帖子的回覆發送。如果你在Mandrill/PHP端(和ajax/jquery端)指定JSON,你的jquery調用應該能夠解析響應。

console.log(response [0])返回'A',因爲這是字符串的第一個字符。

在查詢端添加'json',在php端添加json_encode($ response)。

+0

就是這樣!非常感謝你Ben! –