You're trying to use the return value from $.getJSON, but that is not where
your data is returned. $.getJSON is asynchronous. That is, it returns
immediately as soon as the request is sent to the server (or even before
that). The return value from $.getJSON (for a same-site request, not a
cross-domain request) is the XMLHttpRequest object. That's why you see
things like onreadystatechange in that object.

Instead, you need to provide a callback function. That function will be
called when your data is ready, with the data passed as an argument to the
function:

    $.getJSON(
        'ajax/set_usr_fields.php',
        { ety_id:etyId },
        function( data ) {
            console.log( data );
        }
    );

-Mike

> From: yerfdoggy
> 
> I'm a jQuery/jSON newby. I'm using jQuery getJSON() to do an 
> AJAX request on a separate PHP script, then I want to use the 
> returned jSON object data to update some form fields. I'm 
> sure this is very basic and common, but I've been reading 
> doco. for days and can't get it to work.
> 
> Here's the local AJAX request - it's sending a local variable 
> to the PHP script and it has no callback function:
> 
> var objJson = $.getJSON('ajax/set_usr_fields.php', {ety_id:etyId});
> 
> Here is what is returned from the PHP script, according to 
> the Firebug console, it's what I intended (I've pasted this 
> into a jSON validator and it is valid jSON):
> 
> {"fields":{"ETY_ID":"2","ETY_NM":"A-001","DISPL_NM":"Sammys","
> LONG_DESC":"Some
> text
> here","C_CODE":"61","A_CODE":"5","PHONE":"778908","FAX":"","AD
> DRESS1":"11
> Ward Park
> Drive","CITY":"Somerton","STATE":"VIC","POSTCODE":"3912","CTRY
> _NM":"Australia","CTRY_CODE":"AU",
> "PLIST":"default","TERRITORY":"Mornington
> Peninsula","CONSULTANT":"galbraid"}}
> 
> I've added the following line to the local javascript to have 
> a look at what's returned (I understand it should be a jSON 
> object, since I used
> getJSON() ):
> 
> console.debug(objJson);
> 
> And here is what the Firebug console shows (sorry, the 
> formatting didn't work too well):
> 
> channel                       [xpconnect wrapped nsIChannel]
> multipart             false
> onerror                       null
> onload                        null
> onprogress            null
> onreadystatechange    null
> readyState            4
> responseText  
> "{"fields":{"ETY_ID":"2","ETY_NM":"A-001","DISPL_NM":"Sealite"
> ,"LONG_DESC":"The
> big Kahuna!","C_CODE"..."
> responseXML           null
> status                        200
> statusText            "OK"
> abort                 abort()
> addEventListener      addEventListener()
> dispatchEvent         dispatchEvent()
> getAllResponseHeaders getAllResponseHeaders()
> getInterface          getInterface()
> getResponseHeader     getResponseHeader()
> open                  function()
> overrideMimeType      overrideMimeType()
> removeEventListener   removeEventListener()
> send                  send()
> setRequestHeader      setRequestHeader()
> 
> So far, not so good. I expected the jSON string to be 
> converted to an object I could access with dot notation 
> (again, because I used getJSON()). In fact, I can't even 
> access the simple jSON string, as I tried this:
> 
> console.log(objJson.responseText);
> 
> I get "undefined"
> 
> I've been stuck on this for days, can anyone point out what 
> I'm not understanding, please?
> --
> View this message in context: 
> http://www.nabble.com/Using-jQuery-to-access-jSON-object-tp211
87201s27240p21187201.html
> Sent from the jQuery General Discussion mailing list archive 
> at Nabble.com.
> 

Reply via email to