Jquery Ajax Responsetext Returns Undefined But When I Log It As An Object It Returns The Ajax Text
Solution 1:
You are kindof right, it is a timing problem. The jqXHR-Object exists, but responseText
doesn't at the time you are assigning the object.
But at the time of logging it does show. You shouldn't see the property of responseText
if you log using console.log(JSON.stringify(track))
.
So, wait for the reply by using a callback for readyStateChange
or alike mechanisms.
The problem comes with the logging functionality (I assume firebug or chrome's inspector?) that does not really freeze the object but show's it as it is at the time you look.
Solution 2:
You are trying to use the results of the ajax call before the call has been completed. Javascript will keep executing all the code after the call has been made. Since you don't have any data, it is null until the call has been completed.
You want to modify your $.get()
and pass it a function to execute when the call has been completed.
$.get("generate_song.php", {track_number: randomNumber}, function(track){
console.log(track);
});
You can also just send the parameters without creating the url.
Post a Comment for "Jquery Ajax Responsetext Returns Undefined But When I Log It As An Object It Returns The Ajax Text"