简体   繁体   中英

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.

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

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

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

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