tags:

views:

158

answers:

5

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

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
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
princyp
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
A: 

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
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
princyp
A: 

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
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 thoughline 1, column 38: Undefined root element: Response [help] <?xml version="1.0" encoding="utf-8"?><Response xmlns:xsi="http://www.w3.org .
princyp
A: 

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
I tried doing this above, and it does the alert for the load, but doesnt show any data, and in firebug the xml feed is still like spinning and timing out
princyp
Please post the whole get request from firebug
streetparade
With which parameters did you called the script above? with myurlishere/xml.php or something else?
streetparade
+1  A: 

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
+1 of course. -
Pekka
yeah its from a different domain, so im searching google for an xml Same Origin Policy work around. What should I do?
princyp
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
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