rf_wilson rf_wilson - 1 year ago 135
jQuery Question

jQuery Ajax returns error in IE9 with RSS XML

I have an ASPX page that generates RSS XML like this:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Title</title>
<link>http://www.example.com/news</link>
<description>An RSS feed for the latest news articles.</description>
<language>en-us</language>
<ttl>60</ttl>
<image />
<lastBuildDate>Thu, 11 Jul 2013 16:44:10 GMT</lastBuildDate>
<item>
<title>The Future of News</title>
<image>/uploadedImages/news/Articles/blog.jpg?n=104</image>
<link>http://localhost/news/Articles/5363/</link>
<pubDate>2029-01-11</pubDate>
<formattedDate>today ago</formattedDate>
<summary>Where will news be in 30 years? Check out what sort of news WE think we'll be making!</summary>
<description />
</item>
...
</channel>
</rss>


I need to call this feed from a jQuery file like this:

$.ajax({
dataType: ($.browser.msie) ? "text" : "xml",
url: newsfeed,
cache: true,
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
success: function (data) {
...


The code works in Firefox and Chrome but fails in IE9. In IE9 it triggers the error condition and displays two alerts that both just say "error".

The "newsfeed" variable has the value of "http://localhost/source/fixed/newsrss.aspx" which I have confirmed by using an alert.

Elsewhere I saw that IE doesn't like the "xml" datatype so has to use "text" instead.

I am running the site from localhost so I there should not be any cross domain scripts.

Answer Source

The solution to this problem involved detecting the web browser that was being used. If the browser is IE then instead of using jQuery AJAX I used IE's XMLHttpRequest. Here is my code:

var xhReq = new XMLHttpRequest();
xhReq.open("GET", "/Source/Handlers/Search.ashx?q=" + val + "&d=" + dateObj.getTime(), false);
xhReq.send(null);
AjaxSuccess(xhReq.responseText);

The AjaxSuccess method contains the same javascript that is called upon successful execution of the jQuery AJAX code.

So I got it working but I don't really know why IE didn't like the jQuery approach.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download