5

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

1
  • If by javascript you mean client side javascript that runs in a browser, then no, there is no way due to the same origin policy restriction which is built into browsers to prevent exactly scenarios like this. Commented Nov 10, 2011 at 5:10

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);
 }

?>

4 Comments

your response seems too complex, why he need to create a service? your php code seems didnot give him any new information?
i'm use the grab.php to fix the cross domain because if he execute '$.ajax({ type: "GET", dataType: "xml", url:"google.com/complete/search?output=toolbar&q=microsoft", success: function(){ //to do when success } });' teh response is forbidden i just give solution where i'm get the same problem and this is work for me.thanks
oh, i got it, you are using your own server as a proxy. maybe clearly state the issue and the solution might be better than concrete code -- just my opinion :)
yeah right i will be update the code to be better.thanks for you attention.:)
0

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); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.