简体   繁体   English

AngularJS $ http.post json请求到处于未决状态的expressjs

[英]AngularJS $http.post json request to expressjs in pending status

I have a problem with pending $http.post requests from angularjs to express when trying to post to mongodb. 我在尝试发布到mongodb时遇到来自angularjs的待处理$ http.post请求表示问题。 My app.js: 我的app.js:

app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.methodOverride());
app.use(app.router);

My route.js for post: 我的route.js发布:

self.routeTable.push({

    requestType : 'post',
    requestUrl : '/getAllPrjProjections',
    callbackFunction : function (request, response) {
        console.log(request.body.pr_code);
        console.log(request.body.high_low);
        console.log(request.body);

        var prjProjectionsDao = require('../Server/Dao/prjProjectionsDao.js');

        prjProjectionsDao.create({
            pr_code : request.body.pr_code,
            high_low : request.body.high_low,
            subs_rate : request.body.subs_rate,
            prj_month : request.body.prj_month,
            prj_value : request.body.prj_value,
            last_saved : request.body.last_saved
        }, function(err, todo) {
            console.log(todo);
            if (err) {
                console.log('bbb');
                response.send(err);
            }
        });
    }
});

In the controller I implement the below function when the user clicks the saveProjections button: 在控制器中,当用户单击saveProjections按钮时,我实现以下功能:

$scope.saveProjections = function() {
    $scope.prj_to_save = {"pr_code":"PGU","high_low":"HIGH","subs_rate":"Paid Subs","prj_month":"2016-10-01","prj_value":"96000","last_saved":"2016-12-07"};
    var data_to_send = JSON.stringify($scope.prj_to_save);

    prjProjectionsService.saveProjections($scope.prj_to_save).then(function(success) {
       // $scope.formData = {};
        console.log(data, 'Blog created.');
    },function (error) {
        console.log("The request failed: " + error.data);

    });
};

and the injected server is the below: 注入的服务器如下:

function saveProjections(prjData){
        return $http.post('/getAllPrjProjections', prjData).then(function (success) {
            console.log('success');
        }, function (error) {
            console.log('error');
        });
    }

The problem is that the request stays in pending status for a long time (attached printscreen) and then it fails with error : " XMLHttpRequest: Network Error 0x2ef3, Could not complete the operation due to error 00002ef3" 问题是该请求长时间处于待处理状态(附加的打印屏幕),然后失败,并显示错误:“ XMLHttpRequest:网络错误0x2ef3,由于错误00002ef3而无法完成操作”

However, data are posted in mongodb. 但是,数据发布在mongodb中。 Could you please help me out? 你能帮我吗? I would greatly appreciate it. 我将不胜感激。

enter image description here 在此处输入图片说明

You are only sending a response in case of error, so also send something in case of success 您仅在出现错误的情况下发送响应,因此在成功的情况下也发送一些响应

function(err, todo) {
    console.log(todo);
        if (!err) return response.send(todo); // or send whatever or respnose.end()
        console.log('bbb');
        response.send(err);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM