getJSON not ready with returning data before showing the data
something strange here: I use a $.getJSON to get data from the database. It returns valid data, but it doesn't show in the inputbox afterwards. When I insert an alert("bla") after the json call, it does show the data. My assumption is that the json call isn't ready yet when showing the data on the page. Is开发者_如何学Python there a way to wait for the json call to finish? This is my code:
$.getJSON("mvc/models/predictions.php?action=getpredictions&jsoncallback=?", function(data) {
// Replace markup on page
prediction[1] = data[0].prediction1;
});
// Create output
<input type='text' name='prediction1' size='5' value='" + prediction[1] + "' />
That's right, you need to populate the input box after the ajax call has returned. You can alter the input value inside the JSON return function, like so:
$.getJSON("mvc/models/predictions.php?action=getpredictions&jsoncallback=?", function(data) {
$('input[name="prediction1"]').val(data[0].prediction1);
});
精彩评论