2014-02-13 2 views
0

어떻게 든 Backbone 모델에서 NodeJS 서비스로 데이터를 보낼 수 없습니다.
백본 모델Backbone 클라이언트에서 NodeJS 서비스에 대한 요청 게시

var Money = Backbone.Model.extend({ 
    url: 'http://localhost:3000/sendCoins', 
    defaults: { 
     fromAddress: "", 
     toAddress: "", 
     amount: "" 
    }, 
    transferMoney: function(req, resp) { 
     //get field values 
     console.log(req.fromAddress); //prints fine 
     this.save(); 
    } 
}); 

var transferMoney = new Money(); 

노드 JS 서비스

var app = express(); 
    app.listen(3000); 
    app.use(express.json()); 

app.use(function (req, res, next) { 

    // Website you wish to allow to connect 
    res.setHeader('Access-Control-Allow-Origin', '*'); 

    // Request methods you wish to allow 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 

    // Request headers you wish to allow 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); 

    // Set to true if you need the website to include cookies in the requests sent 
    // to the API (e.g. in case you use sessions) 
    res.setHeader('Access-Control-Allow-Credentials', true); 

    // Pass to next layer of middleware 
    next(); 
}); 


app.post('/sendCoins', function(req, res) { 
    console.log(req.body.toAddress); 
    console.log(JSON.stringify(req.body)); 
    console.log(req.body.amount); 
}); 

I 백본보기 console.log(JSON.stringify(req.body)); 인쇄 {"fromAddress":"","toAddress":"","amount":""}로부터의 요청을 게시.

+1

귀하의 백본 모델이 비어 그것을 당신이 제공하는 기본값을 보냅니다 작동하는지 app.post 위에 다음 코드를 추가합니다. – jsantell

+0

나는 express.bodyParser()를 사용하지 않고 있다고 생각합니다. –

답변

0

M Omary가 언급했듯이 req.body에 액세스하려면 본문 파서를 사용해야합니다. 나는 당신을 위해 작동하지 않는 것을 이해하지 못하고,

app.use(express.bodyParser());