I'm trying to access and display the data stored in a particular url. But my code wasn't running correctly. Any suggestion for this?
function getData( theURL ) {
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theURL, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}
var url = "https://cloudant.com/futon/document.html?acharya%2Ftoxtweet/ff558f75077e8c758523cd3bd8ffdf88";
getData(url);
-
3What does "wasn't running correctly" mean?Glenn Slaven– Glenn Slaven2012年12月19日 04:17:42 +00:00Commented Dec 19, 2012 at 4:17
-
Are you sure you are not violating xmlhttprequest same origin policy?Akhil Sekharan– Akhil Sekharan2012年12月19日 04:20:50 +00:00Commented Dec 19, 2012 at 4:20
-
3You can not send ajax request to cross domain, it should be same domain. you have to use iframe technique the same technique also used by facebook.Kashif Naseem– Kashif Naseem2012年12月19日 04:22:11 +00:00Commented Dec 19, 2012 at 4:22
-
can u explain bit more what u actually wntNipun Jain– Nipun Jain2012年12月19日 04:23:42 +00:00Commented Dec 19, 2012 at 4:23
-
the URL contains info. I wanted to display those info on a page.carebear– carebear2012年12月19日 04:38:12 +00:00Commented Dec 19, 2012 at 4:38
3 Answers 3
XMLHttpRequest only works on the same domain.
If you have a server-side setup, you could proxy the desired page so it arrives from your server.
Comments
Since XMLHttpRequest doesn't allow cross domain requests, I believe you can use three solutions. The usability will be depend on the services you are integrating.
- Use JSONP. If the external URL supports JSONP responses you can use that directly to call cross domains. http://en.wikipedia.org/wiki/JSONP
- Server Side Proxy (As explained by @Kolink)
- Server side scripting. Assume you use PHP at your server side. You can call the external URL via PHP then output relevant data to your page within your domain. The way you have to do this is depending on the server side scripting language.
Comments
Another and pure-js solution is to utilize YQL, checked: http://christianheilmann.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/ (scroll to the bottom for complete script) - works fine.