简体   繁体   English

无法在angularjs服务中通过$ http(AJAX)发送请求

[英]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. 我尝试通过我的服务中的$ http(AJAX)发送请求以进行用户会话是否存在。 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. 我是angularjs的新手。 If there any mistake please ignorance. 如果有任何错误,请无知。

At first check your response 首先检查您的回应

console.log(response); console.log(response);

and check what value get in your response 并检查您的response会获得什么价值

if response.status value is SUCCESS then check 如果response.status值为SUCCESS,则检查

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

instead of if(response.status == 'SUCCESS') 而不是if(response.status == 'SUCCESS')

and ensure that you have data in response.userdata 并确保您有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. 因此,如果两个值不相同,则类型===将返回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 希望这对您有用:D

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

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