0

This is my code:

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

And this is the output of these console.dir lines: (Apparently I can't use images so I'll have to write it out.)


CHROME:

Object

adeudo: "840"

descuento: "0"

total: "840"

[object Object]


FIREFOX:

adeudo: "840"

descuento: "0"

total: "840"

adeudo: "840"

descuento: "0"

total: "840"


So the JSON object does get copied into the global variable in firefox, but in chrome I can't use the JSON as a global variable, if I try to print for example: accountStatus.total I get "undefined" as the value. What am I missing?

This is the full response: {"id_cuenta":"79","subtotal":"840","descuento":"0","total":"840","pagado":"0","adeudo":"840","fecha":"2014-09-10"}

Yozki
  • 73
  • 1
  • 2
  • 13
  • 1
    why are you doing `status = account`? don't you want to set `accountStatus = account`? – bbuecherl May 29 '14 at 19:30
  • See http://stackoverflow.com/questions/23667086/why-is-my-variable-undefined-after-i-modify-it-inside-of-a-function-asynchron?newsletter=1&nlcode=97716%7c4ba7 – Barmar May 29 '14 at 19:33
  • Can you include the original JSON response? The value of any properties will depend on that and it may consist of multiple objects. So, the root object may not have a `total` property, but the objects it contains might. E.g.: `accountStatus[0].total`. [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Jonathan Lonowski May 29 '14 at 19:34
  • Yup yup, sorry, I edited it. I had to modify the code a bit to keep it simple for the question. – Yozki May 29 '14 at 19:34
  • Where/how are you trying to use the global variable? – Felix Kling May 29 '14 at 20:02
  • Several times on different functions. – Yozki May 29 '14 at 20:09
  • Keep in mind that `accountStatus` is not populated until the Ajax success callback is executed. Executing any other functions that need to access `accountStatus` before that won't work. – Felix Kling May 29 '14 at 21:45

1 Answers1

0

It doesn't look like accountStatus is being used. I would change that to status.

Kayla
  • 485
  • 5
  • 14