$.ajax conversion to $http.post or get angular

105 Views Asked by At

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";
    }
}
1

There are 1 best solutions below

0
On

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