I am conversant with PHP but I am a beginner with AJAX, and JSON is driving me crazy.
My PHP script delivers a simple JSON string, e.g.
{"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} ] } I
retrieve it as follows:
var fillDataMasterTable = $.ajax({
url: "/php/PrepareGoogleDataTable.php" +"?"+
"SensorString="+SensorString + "&"+
"TableType=" + TableType +"&"+
"NoAbscissaBins=" + NoAbscissaBins + "&"+
"DataTableName=" + DataTableName +"&"+
"TimeUnit=" + TimeUnit +"&"+
"TimeStart=" + TimeStart,
dataType:'json',
async: false
}).responseText;
$('#debug_div').html(fillDataMasterTable);
That works fine, and I get my JSON string back. But how can I access the individual values? I tried:
$('#debug_div').html(fillDataMasterTable.bindings);
but that just doesn’t work. I am certainly doing something abysmally stupid here, and I am a bit ashamed – but I do need some hand-holding to get myself started..
reponseTextis a property ofXMLHttpRequestobject,and it will not be aJSONobject after request.It’ll be aString.And it does not metter,whether you specify"dataType"as"json".So you need to convert it to json object:
and then you can access properties like this: