【发布时间】:2014-05-29 19:28:17
【问题描述】:
这是我的代码:
var accountStatus;
function getAccountStatus()
{
$.ajax({
type: "POST",
url: "/getAccountStatusJSON.php",
async: false,
data: "id_alumno=1",
dataType: "json",
success: function (account)
{
console.dir(account);
accountStatus= account;
console.dir(accountStatus);
}
});
}
这是这些 console.dir 行的输出: (显然我不能使用图像,所以我必须把它写出来。)
铬合金:
对象
adeudo: "840"
descuento: "0"
总计:“840”
[对象对象]
火狐:
adeudo: "840"
descuento: "0"
总计:“840”
adeudo: "840"
descuento: "0"
总计:“840”
所以 JSON 对象确实被复制到了 firefox 中的全局变量中,但是在 chrome 中,如果我尝试打印例如,我不能将 JSON 用作全局变量: accountStatus.total 我得到 "undefined" 作为值。 我错过了什么?
这是完整的回复: {"id_cuenta":"79","subtotal":"840","descuento":"0","total":"840","pagado":"0","adeudo":"840 ","fecha":"2014-09-10"}
【问题讨论】:
-
你为什么要
status = account?你不想设置accountStatus = account吗? -
能否包含原始 JSON 响应?任何属性的值都取决于它,它可能由多个对象组成。因此,根对象可能没有
total属性,但它包含的对象可能。例如:accountStatus[0].total。 Access / process (nested) objects, arrays or JSON -
是的,抱歉,我编辑了它。我不得不稍微修改一下代码以保持问题的简单性。
-
您在哪里/如何尝试使用全局变量?
标签: javascript jquery json google-chrome