[jQuery] Using jQuery to access jSON object

[jQuery] Using jQuery to access jSON object


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":"","ADDRESS1":"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-tp21187201s27240p21187201.html
Sent from the jQuery General Discussion mailing list archive at Nabble.com.

























































    • Topic Participants

    • mg