我正在制作这个游戏,我正在使用 ajax 调用 php 从数据库中获取项目,但我收到错误,所以预加载器不会消失,当我在 Chrome 上查看资源时出现此错误:"未捕获的类型错误:无法读取 null 的属性'team'"。知道为什么要这样做吗?Jquery:
function appendTeam(){
$.ajax({
url : _path + "/core/ajax.php",
type : 'POST',
data : { f: 'getTeam'},
dataType : 'json',
success : function(data) {
if(data) {
return false;
} else{
var count = 0;
$.each(data.team, function(i, c){
// check
if(!$('#'+c)) return true;
var element = $('#'+c);
$('input[name="s'+i+'"]').val(element.attr('id'));
$('.slot.'+(i+1)).append(element);
element.data('prevParent', $('.slot.'+(i+1)));
count ++;
});
appendStatus(count);
setTimeout(function(){
$('#preloader').fadeOut('fast',function(){
$('#preloader').remove();
popUp('match');
});
}, 2000);
}
}
});
}
菲律宾语:
if (isset($_POST['getTeam'])) {
$team = array();
$sql = mysql_query("SELECT * FROM accounts WHERE id = '1'") or die(mysql_error());
while ($teams = mysql_fetch_array( $sql )) {
$chara1 = $team['cid1'];
$chara2 = $team['cid2'];
$chara3 = $team['cid3'];
}
$team = json_encode(array(
'chara1' => $chara1,
'chara2' => $chara2,
'chara3' => $chara3
));
echo $team;
}
我希望它与团队相呼应,如果他有一个已经在这些div 元素中选择了团队:
<div id="droppable_slots" class="current_team">
<div class="slot 1">1</div>
<input type="hidden" name="s0" value="10">
<div class="slot 2">2</div>
<input type="hidden" name="s1" value="7">
<div class="slot 3">3</div>
<input type="hidden" name="s2" value="3">
</div>
你在帖子数据中寻找错误的键——它应该是"f",而不是"getTeam"。 (请注意,Ajax 调用正在发送data : { f: 'getTeam'}
。 大概你的意思是:
if (isset($_POST['f']) && $_POST['f'] == 'getTeam')
另一个错别字:当有数据时if (data) return false
会返回 false -- 应该是相反的:
if(data && data.team) {
// do stuff
}
我认为你的问题就在这里:
if (data) {
return false; // returns false if data exists
} else {
// data does not exist in this block
var count=0;
$.each(data.team ...
... //
}
基本上,因为你的if/else块没有正确形成。 这应该可以解决它...
if (!data) {
return false;
} else {
... // work with data here
}
你还需要这个:
mysql_fetch_array($con, $sql);
如果要使用过程函数,则需要将连接资源 ($con = mysql_connect) 传递给函数。 如果使用面向对象的方法,则无需将资源作为参数传递。