tags:

views:

226

answers:

3

I am trying to grab information from an external xml with the following code. It only worked when I uploaded same file to my servers. Why cant I grab information from an external url?

<script language="javascript">
// This script uses jQuery to retrieve the news XML file and display the contents
$(document).ready(function(){

 $.ajax({
     type: "GET",
  url: "www.simplyprofound.com/samples/xml_jquery/sample.xml",
  dataType: "xml",
  success: function(xml) {
   $(xml).find('item').each(function(){
    var title = $(this).find('title').text();
    var source = $(this).find('source').text();
    var description = $(this).find('description').text();
    $('<div class="news_title"></div>').html(title).appendTo('#news_wrap');
    $('<div class="news_source"></div>').html(source).appendTo('#news_wrap');
    $('<div class="news_description"></div>').html(description).appendTo('#news_wrap');
   });
  }
 });
});
</script>
A: 

Put http:// in front of your URL, like so:

http://www.simplyprofound.com/samples/xml_jquery/sample.xml

Otherwise, it tries to find a file named sample.xml in a directory of your site named /www.simplyprofound.com/samples/xml_jquery/.

Tim S. Van Haren
I tried that too.
A: 

I believe it's because the jQuery AJAX stuff uses XMLHTTPRequest (as most do), which doesn't allow you to make requests to domains that aren't your own (as a security feature).

Mike Trpcic
anyway to fix it? one of my customers wants me grab information from xxx.com to yyy.com without showing the url. problem here is I cant use server side scripting on yyy.com's servers.
Make your frontend script go to xxx.com/something, and on the serverside grab the content via whatever tools/functions are available to you in the language being used.
Mike Trpcic
A: 

You cant do x-domain xhr requests. You can either use a server side proxy on your side, so you would make the ajax call to a page on your server which would make the remote request and respond the file contents or if the endpoint supports json-p you can set the dataType to jsonp and request the data.

redsquare
"if the endpoint supports json-p you can set the dataType to jsonp and request the data." I went with this option. is this a valid json, if not, why not? :: http://www.pangeaadvisors.org/sep123/blog.cs.asp?Process=ViewBlog
It is not valid. You have a comma just before the closing square bracket ]. Check for valid json here http://jsonlint.com/
redsquare
hi, its valid now. but the code in my first message is still not grabbing the values. http://www0.gsb.columbia.edu/students/organizations/sec/conference2009/blog.htm
it will not as it is not xml. You will need to iterate the json using $.each
redsquare
see http://ajaxian.com/archives/ajaxian-featured-tutorial-parse-json-with-jquery-and-javascript or google "parse json with jquery"
redsquare