Siddharth Gharge
Siddharth Gharge

Reputation: 112

http post response is null

I'm sending http post request to REST api, I'm getting status ok response from server but in this script, it always runs 'myError' function. In backend everything is running fine without any error. In error function also response value remains undefined.

var toDoApp = angular.module('toDoApp');

toDoApp.factory('registrationService', function() {
var register = {};

register.registeruser = function(user, $http) {
    $http({
        method : "POST",
        url : 'register',
        data : user
    }).then(function mySuccess(response) {
        console.log("success");
    }, function myError(response) {
        console.log("error");           
    });

}

return register;
});

Upvotes: 0

Views: 609

Answers (3)

Newaz Sharif
Newaz Sharif

Reputation: 424

Error is showing because you did not inject $http service to to your toDoAppfactory not in your registeruser function . You should inject $http service to your factory. like :

toDoApp.factory('registrationService', function($http)

And your function registeruser should be like

register.registeruser = function(user) {
    $http({
        method : "POST",
        url : 'register',
        data : user
    }).then(function mySuccess(response) {
        console.log("success");
    }, function myError(response) {
        console.log("error");           
    });

}

Upvotes: 0

gaurav bankoti
gaurav bankoti

Reputation: 224

Do some needful correction.

var toDoApp = angular.module('toDoApp',[]);

toDoApp.factory('registrationService', function($http) {
var register = {};

register.registeruser = function(user) {
    $http({
        method : "POST",
        url : 'register',
        data : user
    }).then(function mySuccess(response) {
        console.log("success");
    }, function myError(response) {
        console.log("error");           
    });

}

return register;
});

Upvotes: 0

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41417

Inject the http service to the factory. Not the registeruser function.

toDoApp.factory('registrationService', function($http) {

Upvotes: 1

Related Questions