Javascript 向Div添加AJAX响应数据
下面的代码有问题,它得到了我想要的数据,但它不会附加到div#playercontent中,而是在我运行它时出现在控制台中 使用PHP、Slim、HTML和JSON。把它弄清楚Javascript 向Div添加AJAX响应数据,javascript,get,onclick,Javascript,Get,Onclick,下面的代码有问题,它得到了我想要的数据,但它不会附加到div#playercontent中,而是在我运行它时出现在控制台中 使用PHP、Slim、HTML和JSON。把它弄清楚 $(文档).ready(函数(){ $.ajax({ 键入:“GET”, 数据类型:“json”, url:“db.php/players”, 成功:showResponse, 错误:淋浴错误 }); }); 调试(“错误”); 功能显示响应(响应数据){ $(“#get1”)。单击(函数GetPlayer(respon
$(文档).ready(函数(){
$.ajax({
键入:“GET”,
数据类型:“json”,
url:“db.php/players”,
成功:showResponse,
错误:淋浴错误
});
});
调试(“错误”);
功能显示响应(响应数据){
$(“#get1”)。单击(函数GetPlayer(responseData){
log('单击图像');
控制台日志(responseData);
$.each(responseData.player,函数(索引,播放器){
log('is');
$(“#playercontent”).append(“玩家位置:“+Player.PlayerPosition+”全名:“+Player.PlayerName+”“+Player.PlayerLastName+”球队名称:“+Player.TeamName”);
//$(“#playercontent”)。追加(“”);
log('Data should output');
});
});
控制台日志(responseData);
}
调试(“你好”);
函数名为ror(){
警报(“对不起,出了点问题。请解决!!!!”)
}
将数据附加到
#playercontent
div的代码位于AJAX请求的成功处理程序中的onclick事件处理程序中。i、 它不会运行,因为当您在.ajax
成功回调中时,单击永远不会发生。尝试将迭代responseData
的代码移到onclick处理程序之外,以便它直接位于showResponse
函数中
还有
console.debug(“错误”)代码>和console.debug(“hello”)代码>消息将显示每个页面请求,并且在.ajax
调用发生之前…请不要使用标记发送垃圾邮件。您的问题与Slim
和PHP
都没有关系。感谢您的帮助,mate@MarcinOrlowskiTry移动showResponse
(文档)中的$函数。就绪(函数(){…}
。在定义函数showResponse
之前,我们需要确保元素playercontent
存在。
$(document).ready(function(){
$.ajax({
type: 'GET',
dataType: "json",
url: "db.php/players",
success: showResponse,
error: showError
});
});
console.debug("error");
function showResponse(responseData){
$("#get1").click(function getPlayers(responseData) {
console.log('Image is clicked');
console.log(responseData);
$.each(responseData.player, function(index, player){
console.log(' is ');
$("#playercontent").append(" </br>Player Position:" +player.PlayerPosition+"</br> Full Name:" +player.PlayerName+ " "+player.PlayerLastName+" </br>Team Name:" +player.TeamName);
// $("#playercontent").append("</li>");
console.log('Data should output');
});
});
console.log(responseData);
}
console.debug("hello");
function showError(){
alert("Sorry, but something went wrong. Fix it!!!!")
}