2017-04-24 18 views
0

次のコードに問題がありますが、必要なデータを取得しますが、実行時にコンソールに表示されるdiv #playercontentに追加されません。DivにAJAX応答データを追加

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

タグでないスパムを行ってください。あなたの質問は、「スリム」でも「PHP」でも関係していません。関数 '$(ドキュメント).ready(関数(){...}' '内部showResponse'を移動してみてください@MarcinOrlowski –

+0

おかげで。私たちは確か要素' playercontent'は、関数の前に存在する 'にshowResponseようにする必要があり'が定義されています。 –

+0

あなたの助けの伴侶のため – alfredo

答えて

0

#playercontent div要素にデータを追加するためのコードは、あなたのAJAXリクエストの成功ハンドラ内のOnClickイベントハンドラ内です。 .ajax成功のコールバック内にいるためクリックが発生しないため、実行されません。 onclickハンドラの外側にあるresponseDataを反復するコードを、showResponse関数内に直接移動してみてください。

またconsole.debug("error");console.debug("hello");メッセージは、すべてのページリクエストが表示され、.ajax呼び出しが発生する前に...

関連する問題