0

I have a returned VALID JSON response as follows:

[{"VegetableId":4,"VegetableCode":"001","CatalogId":14,"Name":"Carrot","Description":"The carrot is a root vegetable, usually orange in colour, though purple, 

Please note that the above response is only part of it but as you can see that it does not have a rootElement. How can I consume this in Sencha Touch.

Here is my proxy code but it does not populate the model.

Ext.define('MyApp.store.Vegetables',{
    extend:'Ext.data.Store',
    config:{
        model:'MyApp.model.Vegetable',
        autoLoad:true,
        proxy:{
            type:'jsonp',
            url:'theurltothejson',
            reader:{
                type:'json',
                rootProperty:null
            }

        }

    }

});

Thanks!

john doe
  • 9,220
  • 23
  • 91
  • 167

3 Answers3

0

If the proxy accepts a filter: parameter you can specify a function that pre-processes the returned json, and transform it into something that the rest of your scripts can handle (say, by wrapping it in a root element).

Tom McClure
  • 6,699
  • 1
  • 21
  • 21
  • Can you give an example? – john doe Dec 11 '13 at 17:18
  • looks like this is not possible in this way. you need to subclass the reader as suggested in this comment thread: http://stackoverflow.com/questions/16852095/re-formatting-json-data-to-fit-into-tree-store but alas, no example code there either. – Tom McClure Dec 11 '13 at 17:30
  • Is there any method (success, response) fired when the response is returned from the web server using jsonp as an option? – john doe Dec 11 '13 at 17:42
  • Weird! If I take the json response and put that in a .json file locally then it works by changing proxy type to 'ajax' instead of 'jsonp' – john doe Dec 11 '13 at 18:30
0

Your server response is JSON, but your proxy is set for JSONP... and there's a big difference.

As Evan points out in his comment, you should only need a JSONP proxy if it's going across domains. If you don't need that, swap it to a JSON proxy. If you do need it, then the response has to be valid JSONP.

arthurakay
  • 5,631
  • 8
  • 37
  • 62
0

That response is readable but is your response JSONP or just JSON? You are using JSONP proxy but you are providing only JSON (least here). JSONP !== JSON.

This is JSON:

[{"foo":"bar"}]

This is JSONP:

SomeCallback([{"foo":"bar"}]);

You can see that there is a large difference.

Here is a fiddle of a store loading an array: https://fiddle.sencha.com/#fiddle/23j

Here is a blog I wrote explaining what JSONP is also: http://mitchellsimoens.com/?p=254

Mitchell Simoens
  • 2,516
  • 2
  • 20
  • 29