2017-04-24 78 views
0

遇到以下代码时遇到问题,它会获取我想要的数据,但它不会附加到div #playercontent,而会在运行时出现在控制台中。将AJAX响应数据添加到Div

使用PHP,Slim,HTML,JSON。清除它。

$(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!!!!") 
} 
+2

请标签没有垃圾邮件。你的问题既不与'Slim'也不与'PHP'相关。 –

+0

感谢您的帮助mate @MarcinOrlowski –

+0

尝试在'$(document).ready(function(){...}'内移动函数'showResponse'。我们需要确保'playercontent'元素存在于函数showResponse '被定义。 – alfredo

回答

0

将数据追加到#playercontent DIV的代码是一个onclick事件处理您的Ajax请求的成功处理程序内的范围内。即它不会运行,因为点击将永远不会发生,因为您处于成功回调中。尝试移动遍历onclick处理函数以外的responseData的代码,以使其直接位于showResponse函数中。

而且console.debug("error");console.debug("hello");消息将显示每一个页面请求和.ajax调用发生之前...