简体   繁体   中英

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. My 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:

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:

$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"

However, data are posted in 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);
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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