首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用res.send返回成功

使用res.send返回成功
EN

Stack Overflow用户
提问于 2015-09-14 05:49:40
回答 1查看 896关注 0票数 0

我使用的是Node and Angualr JSexpress-4的结合

在我的代码中,我试图通过res.send发送res.send。我能够得到newSourceId,但是当我使用res.send发送它时,它会进入error,而不是进入success

我也试过使用res.sendStatus(newSourceId),但没有运气!

以下是Node JS部分:

代码语言:javascript
复制
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);
            }); 
 });
});

这是角JS部分:

代码语言:javascript
复制
$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:我不能更改代码的角度部分,需要在Node部分进行更改。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-14 06:32:22

是的,您可以设置json响应,尝试如下

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32558029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档