0

Im attempting to pull in an xml feed that I can load up with php and simpleXML and I can view the direct link, but when I try to use jquery and GET it just times out and I never get a response, the error that comes back is undefined.

Here is the code im using

$.ajax({
type: "GET",
url: "myurlishere",
dataType: "xml",
timeout: 1000,
contentType: "text/xml",
success: function(xml) {
       alert("in");
  },
   complete: function(XMLHttpRequest, textStatus) {
               alert(textStatus);
   },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
       alert(errorThrown);
    }
 });
Josh Davis
  • 28,400
  • 5
  • 52
  • 67
prestonparris
  • 237
  • 1
  • 4
  • 15

5 Answers5

2

Is the URL in the same domain as the page with your Javascript in it? If not, then the browser won't let your page access it with simple AJAX.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • yeah its from a different domain, so im searching google for an xml Same Origin Policy work around. What should I do? – prestonparris Feb 02 '10 at 22:46
  • The normal way is to build a PHP script that runs on your server, makes the request to the external resource, and returns the results to you. – Pekka Feb 02 '10 at 22:47
  • Check out http://stackoverflow.com/questions/752319/cross-domain-ajax-request-with-jquery-php http://stackoverflow.com/questions/1489373/how-to-use-jquery-ajax-for-an-outside-domain – Pekka Feb 02 '10 at 22:48
0

The code looks o.k. to me. The notable difference between PHP and JQuery, though, is that PHP is executed on your web server, and JQuery in your visitor's browser. Maybe the URL you use is accessible only to the PHP script on the web server.

Does it work when you type in the URL into your browser address bar?

What kind of URL is it? It doesn't start with localhost, does it?

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • yeah it works when I go straight to it, it does not say localhost , its a link to a feed on a webserver that i can view, and access with php, its just not doing anything when i try to request it with jquery though – prestonparris Feb 02 '10 at 22:27
  • Ahh of course, the Same Origin Policy. That won't work, you will need a proxy on your local web server. @Pointy got it right. – Pekka Feb 02 '10 at 22:35
0

Often these issues stem from url mixups: e.g. a relative URL is specified, but it's off by 1 directory, etc.

Also -- I notice you're specifying a timeout of 1 second. I don't know what sort of URL you're hitting... is this enough time?

Drew Wills
  • 8,408
  • 4
  • 29
  • 40
  • yeah , not sure, im using a direct link to an xml feed on a server that i can view directly if i copy and paste it into the browser. Even after I increase the timeout time I get the same thing – prestonparris Feb 02 '10 at 22:25
0

In the script that you are trying to access... Before you print out any of your XML data, are you outputting XML headers? i.e.:

header("Content-Type:text/xml");

=====

To fix the domain issue, create a .php file on your server like this:

<?php

$xml = file_get_contents("http://www.the_other_domain.com/feed.xml");

echo $xml;

?>

Then point your AJAX call to that, instead.

tambler
  • 3,009
  • 2
  • 23
  • 26
  • im not sure I do not have access to what is actually producing the feed, here is what i get when i try to validate it though line 1, column 38: Undefined root element: Response [help] – prestonparris Feb 02 '10 at 22:37
0

Did you tryed

$.load(); 

$("#xml").load("myurlishere/xml.php", {pamameters: 25}, function(data)
{
   alert("Data Loaded"+data);
});

Have a look at http://api.jquery.com/load/

streetparade
  • 32,000
  • 37
  • 101
  • 123