Search code examples
javascriptapibackbone.jsstackmob

stackmob error on callback


I am using stackmob for few days and it is still new and there is no much information on the web. I have encountered this issue: I won't to get list of all the store name I have in my stackmobe "db" the scheme called store and here is the code I am using:

var Store = StackMob.Model.extend({
    schemaName: 'store'
});

 var stores = new Store();
stores.fetch({
    success: function(collection) {
        //after we've gotten all the Todo items from StackMob, let's see what we have!
        console.debug(collection.toJSON());
        alert(collection.toJSON());
    },
    error: function(collection, response){
        alert(collection.toJSON());
    }
}); 

I am getting this errors in my chrome log:

OPTIONS http://api.stackmob.com/store 400 (Bad Request) 
XMLHttpRequest cannot load http://api.stackmob.com/store. Origin http://127.0.0.1:4567 is not allowed by Access-Control-Allow-Origin.

Any help would be appreciated!


Solution

  • May I ask what environment you're running in (chrome, firefix, safari, opera, phonegap)? By default, the JS SDK is configured to make AJAX calls to a relative path. e.g. "/store" It looks like somehow it's configured to hit "api.stackmob.com" directly, which only happens in these cases:

    • the JS SDK detects that you're using PhoneGap
    • you've specified the URL to hit api.stackmob.com directly via the init:

      StackMob.init({ ... apiURL: 'http://api.stackmob.com/' })

    The first question I ask myself is how it's making a call to api.stackmob.com while it looks like you're running on the Python server: (I assume that's what it is because I'm seeing 127.0.0.1:4567 in your original post)

    Another thing that Dennis Rongo pointed out above is your Python version. We tested with 2.7 but it sounds like he's noting an issue with 3.3. But it's still unexpected that you're hitting api.stackmob.com from 127.0.0.1:4567. Do you have apiURL: '...' set in your init? If so, I would try removing that.

    Setting apiURL will make the SDK call api.stackmob.com directly (api.stackmob.com/store), and the browser will behave like it would for any regular AJAX call and block it if it's going cross domain, giving the error you see.

    Well, let me know! We definitely want to get you going!

    Erick

    PS I think you're also trying to get more than one store. StackMob.Model only represents one store. You want StackMob.Collection.

    e.g.

    var Store = StackMob.Model.extend({ schemaName: 'store' });
    var Stores = StackMob.Collection.extend({ model: Store });
    
    var stores = new Stores();
    stores.fetch({
      success: function(results) {
        console.log(results.toJSON()); //should print out array of JSON objects representing your store items.
      }
    });
    

    More info: http://developer.stackmob.com/tutorials/js/Query-an-Object