Reputation:
jQuery has the $.getJSON() function that I use to load json files from other domains like so:
$.getJSON('http://somesite.com/file.js', function(output) {
// do stuff with the json data
});
I was wondering if I can do the same with xml files from other domains or do I have to use a server side language for that?
This is the xml document I would like to load:
http://google.com/complete/search?output=toolbar&q=microsoft
Upvotes: 5
Views: 3642
Reputation: 8295
I agree with @viyancs , simply speaking if you want to get xml of other domain, there is a cross-domain-restriction, the way to solve this is create a proxy, so the request process is:
1. use $.ajax to request your proxy(with the real xml url you want to access).
2. your proxy retrive the xml url content.
3. your proxy returns the content to your $.ajax call.
For more detail have a look at: http://developer.yahoo.com/javascript/howto-proxy.html
BTW: why you dont have to do this for JSON? it is a technique called JSONP.
Upvotes: 2
Reputation: 652
If you really don't have the ability to use a proxy with a cache (which is proper etiquette), you can use something like YQL as a JSONP proxy service. You'll eventually hit a limit without an API key.
// query: select * from xml where url='http://google.com/complete/search?output=toolbar&q=microsoft'
var xml_url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D'http%3A%2F%2Fgoogle.com%2Fcomplete%2Fsearch%3Foutput%3Dtoolbar%26q%3Dmicrosoft'&diagnostics=true"
$.get(xml_url,function(xml){ console.log(xml); });
Upvotes: 0
Reputation: 2339
you can try this
$.ajax({
type: "GET",
dataType: "xml",
url:"localhost/grab.php",
success: function(){
//to do when success
}
});
1) make service as proxy to get content from url example in grab.php code:
<?php
$url = 'http://google.com/complete/search?output=toolbar&q=microsoft';
$parsing = parse_url($url);
$scheme = $parsing[scheme];
$baseurl = basename($url);
$strbase =$baseurl;
$finalUri = $scheme .'://' .$strbase;
$handle = fopen($finalUri, "r",true);
// If there is something, read and return
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
?>
Upvotes: 0