Reputation: 124
I'm trying to make a generic function that allows me to get data from different sources at the same time.
I based my solution on this post, and ended up with this:
var servicesURL = 'http://www.somedomain.com/services/xml_proxy.php?url=';
function getExternalData(service, callback) {
$.ajax({
type: 'GET',
url: servicesURL+service,
dataType: 'jsonp',
jsonpCallback: 'myfunction',
success: function(data) { callback(data); },
error: function(jqXHR, textStatus, errorThrown) { console.log(textStatus+': '+errorThrown); }
});
}
getExternalData('data1.xml', function(data) {
console.log(data)
});
getExternalData('data2.xml', function(data) {
console.log(data)
});
Here's the code of the proxy I'm using:
<?php
header('Content-type: application/json');
$url = $_GET['url'];
echo 'myfunction('.json_encode(simplexml_load_file($url)).')';
?>
It works fine when I make a single call to the function, but when I make more that one call (as I did above), I get the following errors:
parsererror: Error: myfunction was not called
Uncaught TypeError: Property 'myfunction' of object [object Object] is not a function
Any help would be highly appreciated
Upvotes: 1
Views: 466
Reputation: 3355
Try putting the second call inside the callback of the first. That should fix the issues you are having.
http://forum.jquery.com/topic/multiple-jsonp-requests-causing-errors
Upvotes: 1