简体   繁体   English

$ .ajax转换为$ http.post或获取角度

[英]$.ajax conversion to $http.post or get angular

am building a voting application in which a particular voter with an email address can only vote once however the code i have here runs too slow please how the i covert the following code to $http.post angular code that returns a response that i can use 我正在构建一个投票应用程序,其中具有电子邮件地址的特定选民只能投票一次,但是我在这里的代码运行太慢,请问我如何将以下代码隐藏到$ http.post角度代码中,以返回我可以使用的响应

$scope.votecheck = function(item,emailid){
    var email = emailid;
    if( typeof item !== 'undefined')
    {
   var jsonData = $.ajax({
    type: "GET",
          url: 'ajax/voters.php?id='+item.ID+'&email='+email,
                dataType: 'text',
                async: false
            }).responseText;
if(jsonData === "CanVote"){

    return true;
}
else{

    return false;
        }   //return "canvote";
    }
}

Use a promise something like this... 像这样使用诺言 ...

    $scope.voteCheck = function(email, id) {
        var deffered = $q.defer();
        $http.get('ajax/voters.php?id='+item.ID+'&email='+email, {
        }).success(function(data) {
            deffered.resolve(data);
        });
        return deffered.promise;
    };

Then call something like this... 然后叫这样的话...

var votePromise = $scope.voteCheck($item.ID, email);
        votePromise.then(function (data) {
                return(data === "CanVote");
})
};

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

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