0
votes

Why if a use a JSON file foo.json my code works but if I change the URL to something.com/foo.json it doesn´t work?

This is working in my project:

var store = Ext.create('Ext.data.Store', {
        model: 'Client',
        autoLoad: true,
        autoSync: true,
        proxy: {
            type: 'rest',
            url : 'clients.json',
            reader: {
                type: 'json'
            }
        }
    }); 

What I want is to replace the static file for an URL:

var store = Ext.create('Ext.data.Store', {
        model: 'Client',
        autoLoad: true,
        autoSync: true,
        proxy: {
            type: 'rest',
            url : 'http://rails-api.herokuapp.com/clients.json',
            reader: {
                type: 'json'
            }
        }
    }); 

The clients.json file is a copy/paste from http://rails-api.herokuapp.com/clients.json it is the same data.

2
You should read carefully the doc. The server that you made request should respond callback by jsonp type. If url that you request on your domain, does not necessary to use jsonp. docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.proxy.JsonP - Oğuz Çelikdemir
yes, but the webservice is intended to be public so I needed to add the callback :) Thanks - M.Octavio

2 Answers

1
votes

Where do you run your application? Are you able to track the http requests? Do you get any output on your javascript console?

If i had to guess I'd say that your issue might be related to CORS => http://en.wikipedia.org/wiki/Cross-origin_resource_sharing.

Edit: Note that you only need to have a look at CORS or use jsonp if you are running your app and the "backend"/api on two different webservers.

Most people will probably...

  • a) ...run the app on the same webserver as the backend or...
  • b) ...use native packaging (cordova, phonegap or sencha cmd s own packaging).

In both cases you can simply use the "normal" ajax or rest proxys.

0
votes

On the server side I had to add the callback, see this question sencha-seems-to-not-like-rails-json also I had to change the type from rest to jsonp and removed some useless code, at the end my code looks like this:

var store = Ext.create('Ext.data.Store', {
        model: 'Client',
        autoLoad: true,
        autoSync: true,
        proxy: {
            type: 'jsonp',
            url : 'http://rails-api.herokuapp.com/clients.json'
        }
    }); 

on the server side:

def index
    @clients = Client.all
    respond_to do |format|
        format.html 
        format.json { render :json => @clients, :callback => params[:callback] }
      end
end