簡體   English   中英

使用res.send返回成功

[英]Returning success using res.send

我正在使用Node and Angualr JS express-4

在我的代碼中,我試圖通過res.send發送newSourceId 我能夠獲取newSourceId,但是當我使用res.send發送它時,它會error而不是success

我也嘗試過使用res.sendStatus(newSourceId)但是沒有運氣!

以下是Node JS部分:

app.post('/addObservationDetail',function(req,res){

var newSourceId = -1;

 Observation.create({obv_source:req.body.source,obv_waveband:req.body.waveband,prpsl_id:req.body.pId}
 ).then(function(result) {

        Observation.findAll({
            attributes: [['obv_id','id']],where:{prpsl_id:req.body.pId},order: [["obv_id","DESC"]],limit: 1
            }) .then(function(rows) {
                console.log("Obv_Id is = "+rows[0].dataValues.id);
                newSourceId=rows[0].dataValues.id;
                console.log("newsrc===="+newSourceId);
                 res.send(newSourceId);
            }); 
 });
});

這是Angular JS部分:

$scope.updateObsDet = function(obs_detail,index) {
            var url = "/";
            if(obs_detail.sourceId == null){
                url += "addObservationDetail";
                $http({
                    method:'POST',
                    url:url,
                    headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'},
                    transformRequest:function(obj){
                        var str = [];
                        for(var p in obj)
                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                        return str.join("&");
                    },
                    data:{source:obs_detail.source,waveband:obs_detail.waveband,pId:$scope.proposal.proposalId}                     
                }).success(function(data){
                    alert("New observation added");       //should come in here
                    updateSourceId(data,index,obs_detail);
                }).error(function(data){
                    alert(data);         //getting in here
                });

            } else {
                url += "updateObservationDetail";
                data = {
                        source:obs_detail.source,
                        waveband:obs_detail.waveband,
                        sourceId:obs_detail.sourceId
                        };
                $http.post(url,data).success(function(data){
                    alert(data);
                }).error(function(data){
                    alert(data);
                });
            }
        };

PS:我無法更改代碼的angular部分,需要在Node JS部分進行更改。

是的,您可以設置json響應試試這個

var http = require('http');

var app = http.createServer(function(req,res){
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ a: 1 }));
});
app.listen(3000);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM