0

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);
Tepken Vannkorn
9,72514 gold badges63 silver badges86 bronze badges
asked Dec 19, 2012 at 4:16
5
  • 3
    What does "wasn't running correctly" mean? Commented Dec 19, 2012 at 4:17
  • Are you sure you are not violating xmlhttprequest same origin policy? Commented Dec 19, 2012 at 4:20
  • 3
    You 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. Commented Dec 19, 2012 at 4:22
  • can u explain bit more what u actually wnt Commented Dec 19, 2012 at 4:23
  • the URL contains info. I wanted to display those info on a page. Commented Dec 19, 2012 at 4:38

3 Answers 3

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.

answered Dec 19, 2012 at 4:22
Sign up to request clarification or add additional context in comments.

Comments

1

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.
answered Dec 19, 2012 at 4:49

Comments

0

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.

answered Dec 19, 2012 at 4:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.