【问题标题】:Impossible to get JSON from Service into angularJs Routing无法将 JSON 从服务获取到 angularJs 路由
【发布时间】:2017-02-10 09:34:14
【问题描述】:

我有这项服务:

 monApp.service('serviceEtatConnexion', function ($http) {
    this.getEtatConnexion = function () {
     return $http.get('backend/backend.php?action=get_etat_connexion');
  };
});

后端 php 直接调用时返回这个值:

{"statuslogin":1}

在我的 angularjs 路由中,我想得到这个值,像这样测试这个值:

resolve:{
    "check":function($window,serviceEtatConnexion,$location){
        var u = serviceEtatConnexion.getEtatConnexion();

        console.log('u est egal a ' +u);
        console.log(u.$$state.statuslogin);
        alert(JSON.stringify(u, null, 4));
       /*   if( u!= '{"$$state":{"status":0}}' ){
             alert("ok");
        }else{
             alert("Vous devez vous identifier pour utiliser cette partie");
            $window.location.href ='http://liseo.lan/dev/login.phtml?mlog=&domain=stuff';   

        } */
    }
    }

绝对没有办法获取 statuslogin json 值,看看我尝试过的所有内容。我总是在控制台中看到“未定义” 我不明白为什么。

关于信息,这是我的 php 函数,它正确地编码了一个 json:

function get_etat_connexion(){

session_start();

include_once( "../../../globprefs.php" );
include_once( "../../../manage_session.php" );

$http_form_vars = count( $_POST ) > 0 ? $_POST : 
                                    ( count($_GET) > 0 ? $_GET : array("") );


if(USER&PASSWORD){

     $reponse['statuslogin']=1;
        echo(json_encode($reponse));


}

else{
     $reponse['statuslogin']=0;
        echo(json_encode($reponse));
}

}

即使这样也不起作用并返回 undefined:

var statuslogin = $http.get('backend/backend.php?action=get_etat_connexion'); console.log('statuslogin est egal a ' +statuslogin);

【问题讨论】:

    标签: javascript php angularjs json


    【解决方案1】:

    $http 服务返回承诺,这意味着仅调用该函数不会返回任何内容,因为承诺尚未解决。看看下面的sn-p

    resolve:{
        "check":function($window,serviceEtatConnexion,$location, $q) {
            var deferred = $q.defer();
            serviceEtatConnexion.getEtatConnexion()
                .then(function(result) {
                    if (result.statusLogin === 1) {
                        // Everything is okay, resolve the promise with the result
                        deferred.resolve(result);
                    } else {
                        // Something went wrong, not logged in. Reject the promise
                        deferred.reject(result);
                    }
                });
            return deferred.promise;
        }
    }
    

    然后您可以在if 语句中添加任何代码来处理您的错误。一个常见的约定是侦听$stateChangeError 事件并在该侦听器中处理重定向等。

    【讨论】:

    • 是的,当此人未登录时,它最终不起作用,我正在尝试您的解决方案
    • 终于可以了,非常感谢,改成if (result.data.statuslogin === 1)
    【解决方案2】:

    您得到undefined,因为您将变量u 分配给$http 服务创建的承诺(最终得到解决)。您真正想要的是等到该承诺得到解决并将变量u 分配给承诺的成功回调返回的响应。修复

    变化:

    var u = serviceEtatConnexion.getEtatConnexion();
    

    收件人:

    var u = null;
    serviceEtatConnexion.getEtatConnexion()
        .then(function(response){
            u = response.data;
        });
    

    现在u 应该等于{"statuslogin":1}

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 2015-01-30
      • 2023-04-09
      • 2021-09-08
      • 1970-01-01
      相关资源
      最近更新 更多