Reputation:
I want to fetch feeds in my webpage. The feed link is : News. I have tried this code :
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Gmail</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.4.4.js"></script>
<script type="text/javascript">
var xmlhttp;
function fetchFeed(){
if(window.XMLHttpRequest){
$.ajax({
type: "GET",
url: "http://timesofindia.feedsportal.com/c/33039/f/533916/index.rss",
success: function(xml) {
var i=0;
alert($(xml).find('item').length);
$(xml).find('item').each(function(){
alert($(this).find('title').text());
i++;
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("XMLHttpRequest: " + XMLHttpRequest + "\nTextStatus : " + textStatus + "\nError Thrown: " + errorThrown);
}
});
}
}
</script>
</head>
<body>
<div id="gmailForm"></div>
<input type="button" value="submit" onclick="fetchFeed()"/>
</body>
</html>
Well, this works properly in IE & gives output as :
But, not working in Chrome :
& also not in FireFox :
Where is my error ?
Upvotes: 0
Views: 2837
Reputation: 113878
This: url: "http://timesofindia.feedsportal.com/c/33039/f/533916/index.rss"
I'm guessing you are not the Times of India.
You're bumping against the same origin policy. Basically, you can only do XMLHttpRequest to the same server the page comes from. It should not even work in IE unless you set the security policy very, very low.
You need to proxy the request on your server either via mod_rewrite/mod_proxy or use a cgi script.
Here's a nice article on YAHOO! Developer Network describing the problem and gives a simple PHP script to do the proxying: JavaScript: Use a Web Proxy for Cross-Domain XMLHttpRequest Calls
Upvotes: 4
Reputation: 6571
If you are using jquery u don't have to care about XMLHttpRequest jquery will take care of it just use
$.ajax({
type: "GET",
url: "http://timesofindia.feedsportal.com/c/33039/f/533916/index.rss",
success: function(xml) {
var i=0;
alert($(xml).find('item').length);
$(xml).find('item').each(function(){
alert($(this).find('title').text());
i++;
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("XMLHttpRequest: " + XMLHttpRequest + "\nTextStatus : " + textStatus + "\nError Thrown: " + errorThrown);
}
});
Upvotes: 1