Ludo
Ludo

Reputation: 5280

trying to fetch datas from express.js using backbone.js

I am trying to build an application with backbone.js and express.js. I have an issue to return the values from express to backbone. I had the same problem with a simple Curl request (i could display req.params on the server side but impossible to get it on the client side, even using JSON.stringify() on it).

I did the same code with a simple echo json_ecode() in php and it works well...

Here is the code of a simple test on the server side:

var express = require("express");
var app = express();

app.get('/user/:id/:pass', function(req, res){
res.status(200);
res.send({"id": "1"});
});

On the client side, i don't get any of the success or error callaback...

 var U1 = new User();
    U1.fetch({
            success: function(data) {
              console.log('User fetched.'); 
            },
            error: function(model, error) {
              console.log('user Error: '+error);
            }
    });

What is wrong with my answer on express.js ?

Thanks !

Upvotes: 0

Views: 765

Answers (2)

Ludo
Ludo

Reputation: 5280

Ok i found the solution by adding res.header("Access-Control-Allow-Origin", "*"); in my express route.

Upvotes: 1

pixelhandler
pixelhandler

Reputation: 625

As long as the User model sets it's url to the same /user/login/password then it should work, see: http://backbonejs.org/#Model-url Did you already create a url method on the User model?

I posted a couple articles on backbone.js and express ... http://www.pixelhandler.com/blog/2012/02/29/backbone-js-models-views-collections-to-present-api-data/ backbone example using API data from express app: http://pixelhandler.com/blog/2012/02/09/develop-a-restful-api-using-node-js-with-express-and-mongoose/

Upvotes: 0

Related Questions