您好我的代码存在问题,它挂起了用户浏览器,从最终用户的角度来看并不好..但是将async转为true会返回undefined,因为服务器还没准备好state返回responseText .. wi ld真的很喜欢有人给我一个想法来恢复这个代码...因为它是在html ng-show @ the font end中执行的
控制器:
$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";
}
}
HTML:
<div class="row text-center">
<div class="col-md-10 col-md-offset-1" ng-show="!votecheck(item,me.email)">
<p class="contest-text voters"><a class="btn btn-info btn-block img-rounded" ng-disabled="true"><b>Thanks For Voting</b></a> </p>
</div>
<div class="col-md-10 col-md-offset-1" ng-show="votecheck(item,me.email)">
<p class="contest-text voters"><a class="btn btn-primary btn-block img-rounded" ng-click="upVote(item,me.email);"><b> VOTE</b></a> </p>
</div>
PHP:
<?php
include('../include/connect.php');
$id = $_GET['id'];
$emailid = $_GET['email'];
$query="select * from voters where votedid='$id' and votersemail ='$emailid'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$rowcount = $result->num_rows;
if($rowcount === 0)
{
echo "CanVote";
}
else{
echo "cantvote";
}
?>
thanx guyz
答案 0 :(得分:0)
你是在混淆jquery和角度吗? Angular本质上是异步的,带有promise的简单$ http.get可以解决问题。
your parameters should be passed here.
var url = 'ajax/voters.php?id='+item.ID+'&email='+email;
$http.get('url')
.success(function(data)
{
// do your operarion here;
})
.error(function(data){
//do what ever u want on error.
});
希望这有帮助。
答案 1 :(得分:0)
首先,您应该使用$http
服务(https://docs.angularjs.org/api/ng/service/ $ http),而不是$.ajax
。
如果我理解你要做什么,那么在你确定当前用户能够投票之前,你不应该显示“投票”链接。这可以这样做:
// TODO : fill canVote object with your item list,
// I guess you're using ng-repeat, and have an array of items.
// This object will be used to determine if the user can vote or not.
// Key : item ; value : boolean
$scope.canVote = {
item1: false
};
$scope.votecheck = function(item,emailid){
var email = emailid;
if( typeof item !== 'undefined')
{
$http.get('ajax/voters.php?id='+item.ID+'&email='+email).then(
function cb_success(response) {
$scope.items[item] = response.data === "CanVote";
}, function cb_failure(response) {
console.error(response);
});
}
}
angular.forEach($scope.canVote, function (status, item) {
$scope.votecheck(item, $scope.me.email);
});
这是你应该如何处理你的HTML:
<div class="row text-center">
<div class="col-md-10 col-md-offset-1" ng-show="canVote[item]">
<p class="contest-text voters"><a class="btn btn-info btn-block img-rounded" ng-disabled="true"><b>Thanks For Voting</b></a> </p>
</div>
<div class="col-md-10 col-md-offset-1" ng-show="canVote[item]">
<p class="contest-text voters"><a class="btn btn-primary btn-block img-rounded" ng-click="upVote(item,me.email);"><b> VOTE</b></a> </p>
</div>
由于你没有提供任何JSFiddle(w / e),我无法测试它。如果您有一些问题,请告诉我。