943,175 Members | Top Members by Rank

Ad:
Aug 31st, 2010
0

Could I download the html text from other website using AJAX?

Expand Post »
I'm a AJAX newbie, I was wondering if I can use the .responseText method, to load the text from other website?

For example, how can I download the data from "http://www.weather.com/weather/today...China+CHXX0049" and display the data in my own way?

It seems that the .responseText/.responseXML does not work here...

thanks for your kindly help!
Last edited by am5a03; Aug 31st, 2010 at 12:51 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
am5a03 is offline Offline
12 posts
since Mar 2010
Aug 31st, 2010
0
Re: Could I download the html text from other website using AJAX?
It depends on the html tag displaying the text and its id or name for example lets say you got an text response which is HTML and the tag displaying this text you want to modify is a DIV tag with id "message" you can work with it like this.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2.  
  3.  
  4.  
  5. // assuming the XMLHttpRequest is xhr
  6. var responsestr=xhr.responseText;
  7. // you have to include the gotten response on the page first, lets say a hidden div with the id "hidden"
  8.  
  9. document.getElementById("hidden").innerHtml=responsestr;
  10. var wantedstr;
  11.  
  12. wantedstr=document.getElementById("message").innerHtml;
  13. //then manipulate the text the way you want it


its kind of shady though but I hope it helps
Reputation Points: 10
Solved Threads: 8
Light Poster
meffe is offline Offline
41 posts
since May 2010
Sep 1st, 2010
0
Re: Could I download the html text from other website using AJAX?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <title>AJAX return text</title>
  3. <head>
  4. <script type = "text/javascript">
  5. function getXMLHTTPRequest(){
  6. var req = false;
  7. try {
  8. req = new XMLHttpRequest();
  9. }catch(err1){
  10. try{
  11. req = new ActiveXObject("Msxml2.XMLHTTP");
  12. }catch(err2){
  13. try{
  14. req = new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16. catch(err3){
  17. req = false;
  18. }
  19. }
  20. }
  21. return req;
  22. }
  23.  
  24. var http = getXMLHTTPRequest();
  25.  
  26. function getServerText(){
  27. var myurl = "http://www.weather.com/weather/today/Hong+Kong+China+CHXX0049";
  28. http.open("GET", myurl, true);
  29. http.onreadystatechange = useHttpResponse;
  30. http.send(null);
  31. }
  32.  
  33. function useHttpResponse(){
  34. if( http.readyState == 4){
  35. var mytext = http.responseText;
  36. alert(mytext);
  37. }else{
  38. document.getElementById('test').innerHTML = 'Not fetched';
  39. }
  40. }
  41.  
  42. </script>
  43. </head>
  44. <body onload="getServerText()">
  45. <div id='test'></div>
  46. </body>
  47. </html>>

thx meffe!

This is what I have, but the alert box just give me some blank msg.
It gives me "null" if I replace http.responseText with http.responseXML
What's wrong with my coding?
Last edited by am5a03; Sep 1st, 2010 at 10:11 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
am5a03 is offline Offline
12 posts
since Mar 2010
Sep 1st, 2010
0
Re: Could I download the html text from other website using AJAX?
nothing is wrong with your code. responseXML might give you null when the returned value from request is not valid XML.

it is better if you create an element in body with style="display: none" attribute then do the parsing from there as meffe suggested above.
Reputation Points: 6
Solved Threads: 19
Posting Whiz in Training
fatihpiristine is offline Offline
283 posts
since Sep 2007
Sep 2nd, 2010
0
Re: Could I download the html text from other website using AJAX?
I've written a simple html page called 'test.html' and it works!
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>HelloWorld</title>
  4. </head>
  5. <body>
  6. <div id='hello'>Hello</div>
  7. </body>
  8. </html>
All the html codes displayed correctly.
I just wondering why I cannot do this when I linked to some other sites outside.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
am5a03 is offline Offline
12 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Setting default value through javascript
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Autofilling a text field based on prior input





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC