M.Octavio
M.Octavio

Reputation: 1808

sencha touch rest proxy url

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.

Upvotes: 0

Views: 691

Answers (2)

Adrian
Adrian

Reputation: 91

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.

Upvotes: 1

M.Octavio
M.Octavio

Reputation: 1808

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

Upvotes: 0

Related Questions