0

I am attempting to print a JSON that I get like this:

jsonfields = $.ajax({
url: "ajax.php?getsensors="+raw.deviceId, 
async: false
}).responseText;

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"

3
  • 2
    ["ping"],["location"],["death"],["birth"],["DeviceInfo"] does not look like valid JSON. Commented Dec 9, 2010 at 22:38
  • I think they are multiple JSONs comma-delimited, thats how the sample data is structured, it renders properly when the data is manually entered into the store. Commented Dec 9, 2010 at 22:40
  • what happens if you add datatype:"json" to your $.ajax call? Commented Dec 9, 2010 at 23:09

1 Answer 1

1

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'], ...
]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.