Print JSON from Variable in Javascript
I am attempting to print a JSON that I get like this:
jsonfields = $.ajax({
url: "ajax.php?getsensors="+raw.deviceId,
async: false
}).responseTe开发者_JAVA百科xt;
into an ExtJS datastore that looks like this:
Ext.grid.dummyData = [
//jsonfields,
//["ping"],["location"],["death"],["birth"],["DeviceInfo"],
['3m Co',71.72,0.02,0.03,'4/2 12:00am', 'Manufacturing'],
['Alcoa Inc',29.01,0.42,1.47,'4/1 12:00am', 'Manufacturing'], ...
So when I alert the variable 'jsonfields' it alerts
["ping"],["location"],["death"],["birth"],["DeviceInfo"]
But it doesn't render to the grid at all, but when I hard-code the line above into the json, it does render. I set my jsonfields var like:
var jsonfields;
This is the first thing I do in the script tag. I know that the value of it is updated due to the alert. So how is it different from displaying the JSON from a pre-set variable compared to when I hard-code it in?
Thanks!
UPDATES: I can also see the response of the ajax request in the chrome developer tools XHR, it seems to be structured the same: ["ping"],["location"],["death"],["birth"],["DeviceInfo"]
alert(typeof jsonfields);
Returns "string"
It would be better to correct the server so it produces valid JSON and then use an Ext.data.JsonStore
If you cannot correct the server here's a very manual solution:
jsonfields = Ext.decode('[' + $.ajax({
url: "ajax.php?getsensors="+raw.deviceId,
async: false
}).responseText + ']');
Ext.grid.dummyData = jsonfields.concat([
['3m Co',71.72,0.02,0.03,'4/2 12:00am', 'Manufacturing'],
['Alcoa Inc',29.01,0.42,1.47,'4/1 12:00am', 'Manufacturing'], ...
]);
精彩评论