prestonparris
prestonparris

Reputation: 237

XML not loading with jquery GET but does load with direct link and php?

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

Upvotes: 0

Views: 1258

Answers (5)

tambler
tambler

Reputation: 3039

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.

Upvotes: 0

Pointy
Pointy

Reputation: 413720

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.

Upvotes: 2

streetparade
streetparade

Reputation: 32888

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/

Upvotes: 0

Drew Wills
Drew Wills

Reputation: 8446

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?

Upvotes: 0

Pekka
Pekka

Reputation: 449415

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?

Upvotes: 0

Related Questions