carebear
carebear

Reputation: 771

Javascript - data accessing

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

Upvotes: 0

Views: 83

Answers (3)

Prasad Rajapaksha
Prasad Rajapaksha

Reputation: 6190

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.

  1. Use JSONP. If the external URL supports JSONP responses you can use that directly to call cross domains. http://en.wikipedia.org/wiki/JSONP
  2. Server Side Proxy (As explained by @Kolink)
  3. 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.

Upvotes: 1

Konrad Kozioł
Konrad Kozioł

Reputation: 45

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.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324610

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.

Upvotes: 3

Related Questions