我有一个PHP脚本,通过Mandrill API成功连接并发送电子邮件。虽然听起来很蠢,但我无法弄清楚如何解析以下JSON响应:
Array
(
[0] => Array
(
[email] => matt@mattblum.com
[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 :(得分:0)
马特 看起来您的PHP数组语法被捕获为文本并作为对您帖子的响应发送。如果在Mandrill / PHP端(以及ajax / jquery端)指定JSON,则jquery调用应该能够解析响应。
console.log(response [0])返回'A',因为这是字符串的第一个字符。
在查询端添加'json',在php端添加json_encode($ response)。