I’m trying to understand jQuery’s getJson(). I want to put the array that it returns into a variable put I don’t know how. I get undefined when I click the start button in this code. I know that everything in the PHP file works so I don’t think the propblem is there.
function getGameData(){
var difLevel = $("input[@name=difLevel]:checked").val();
var gameData = (function(){
var json = null;
$.getJSON('/Spelling4/core/proof_Test_JSON_process4.php',{difNo : difLevel},function(data){
json = data;
});
return json;
});
return gameData;
}
$('#startButton').click(function(e) {
var gameData = getGameData();
e.preventDefault();
alert(gameData[1]);
});
Ajax is asynchronous, when you call your function and if the ajax request is not completed your function returns
nullyou should use deferred object. Note that your attribute selector is wrong you should remove the@.