vineet
vineet

Reputation: 14266

Unable to send request via $http(AJAX) in angularjs service

I try to send request via $http(AJAX) in my service for user session exist or not. But it not get any result.

Code:-

eshopApp.service('userDataService', function ($rootScope,$http) {


 $http({
    method: "POST",
    url: "services/login.php"
  }).success(function (response) {
    if (response.status == 'SUCCESS') {
        $rootScope.userData = response.userdata;
    }
    console.log($rootScope.userData);
  }).error(function (error) {
    alert('Unable to login. Please try later.');
 });
});

I am novice in angularjs. If there any mistake please ignorance.

Upvotes: 0

Views: 63

Answers (2)

Shaishab Roy
Shaishab Roy

Reputation: 16805

At first check your response

console.log(response);

and check what value get in your response

if response.status value is SUCCESS then check

if (response.status === 'SUCCESS')

instead of if(response.status == 'SUCCESS')

and ensure that you have data in response.userdata

N.B: The == operator will compare for equality after doing necessary type conversions. If use === operator no need to type conversion. so if two values are not the same type === will return false.

Upvotes: 1

jonode
jonode

Reputation: 797

This is how I generally format mine.

$http.post('services/login.php', POST-DATA-GOES-HERE-IF-ANY)
.success(function(response){
    if (response.status === 'SUCCESS') {
        $rootScope.userData = response.userdata;
    }
    console.log($rootScope.userData);
}).error(function (error) {
    alert('Unable to login. Please try later.');
});

Note: Three '=' instead of two. Note: I'd console log the response til you've got it functioning correctly.

Hopefully this works for you :D

Upvotes: 0

Related Questions