简体   繁体   中英

$.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

$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");
})
};

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