Reputation: 2070
A simple ajax call, the php echos some xml text I need to appear in JPayer where myAJAXVariable is.
I need something similar to php echo which simply prints the data.
I simply want myAJAXVariable to display the data from getxml2.php
$(document).ready(function() {
var myAJAXVariable = $.get("http://www.freeenergymedia.com/getxml2.php", function(data){
return data; <!--here I am attempting to print the contents of getxml2.php as data-->
});
new jPlayerPlaylist({
jPlayer: "#jquery_jplayer_1",
cssSelectorAncestor: "#jp_container_1"
}, myAJAXVariable, {
swfPath: "js",
supplied: "mp3, oga",
wmode: "window"
});
});
Upvotes: 0
Views: 3284
Reputation: 3125
You could also consider using the ajax method (which is much more configurable than get, read this thread Difference between $.ajax() and $.get() and $.load() form additional details) to write your own `getData' function. Then you can use your function to retrieve data in your code.
I personally use this code to retrieve json data from a url, in my backbone.js app:
function getJSONData(myurl)
var data;
$.ajax({
async: false, //thats the trick
url: myurl,
dataType: 'json',
success: function(response){
data = response;
}
});
return data;
}
you can use different datatypes (xml, json, script, or html) if you need (look here http://api.jquery.com/jQuery.ajax/) And then, in your case, you could use it in the PlayerPlaylist object:
new jPlayerPlaylist({
jPlayer: "#jquery_jplayer_1",
cssSelectorAncestor: "#jp_container_1"
}, getJSONData("http://www.freeenergymedia.com/getxml2.php"), {
swfPath: "js",
supplied: "mp3, oga",
wmode: "window"
});
Hope this helps.
Upvotes: 1
Reputation: 1039548
Due to the asynchronous nature of AJAX (which is what the first A stands for) you should consume the result of an AJAX call only inside the success callback, otherwise the data will not be initialized yet:
$(document).ready(function() {
$.get("http://www.freeenergymedia.com/getxml2.php", function(data) {
// Only here you should use the data variable as it is here and only here
// that it is assigned
new jPlayerPlaylist({
jPlayer: "#jquery_jplayer_1",
cssSelectorAncestor: "#jp_container_1"
}, data, {
swfPath: "js",
supplied: "mp3, oga",
wmode: "window"
});
});
});
or create a function:
var initializejPlayer = function(data) {
new jPlayerPlaylist({
jPlayer: "#jquery_jplayer_1",
cssSelectorAncestor: "#jp_container_1"
}, data, {
swfPath: "js",
supplied: "mp3, oga",
wmode: "window"
});
}
and then trigger the AJAX call:
$(document).ready(function() {
$.get("http://www.freeenergymedia.com/getxml2.php", initializejPlayer);
});
Upvotes: 2