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 "" and display the data in my own way?

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

thanks for your kindly help!:)

Dani AI

Generated

Short answer: a browser will not let an in-page AJAX script fetch arbitrary HTML from another site unless the remote server explicitly permits it. The blank alert seen by is the usual symptom: the request is either blocked by the browser or returns nothing usable, and as noted responseXML will be null if the returned content is not well-formed XML. is correct that once you actually have HTML you can parse it, but that presumes the request was allowed.

This is enforced by the Same‑Origin Policy; cross‑origin requests require the remote server to opt in (CORS). See the MDN overviews for details: Same-origin policy and CORS. Typical browser feedback is a console error like “Cross-Origin Request Blocked”; use the network/console panels to confirm whether the request was sent, what status code returned, and whether CORS headers are present.

Workarounds and safer approaches:

  • Use a server-side proxy: have your server fetch the remote page and return it to the browser. This avoids SOP and lets you sanitize or cache results. Validate and allowlist target hosts to prevent SSRF.
  • Use a public API (e.g., a weather API) instead of scraping HTML — APIs return structured data and are intended for integration.
  • JSONP only works if the remote site supports it and is limited to GET.
  • Beware legal/ToS and anti‑scraping protections, and don’t inject untrusted HTML into the DOM without sanitizing (scripts/CSS can break your page or create XSS).

Example server-side workflow (concept): client requests /proxy?url=..., server fetches that URL (curl/fetch), returns safe output. Validate input, enforce allowlists, and inspect response headers first.

Recommended Answers

All 4 Replies

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.

// assuming the XMLHttpRequest is xhr
var responsestr=xhr.responseText;
// you have to include the gotten response on the page first, lets say a hidden div with the id "hidden"

document.getElementById("hidden").innerHtml=responsestr;
var wantedstr;

wantedstr=document.getElementById("message").innerHtml;
//then manipulate the text the way you want it

its kind of shady though but I hope it helps

<html>
	<title>AJAX return text</title>
	<head>
		<script type = "text/javascript">
			function getXMLHTTPRequest(){
				var req = false;
				try {
					req = new XMLHttpRequest();
				}catch(err1){
					try{
						req = new ActiveXObject("Msxml2.XMLHTTP");
					}catch(err2){
						try{
							req = new ActiveXObject("Microsoft.XMLHTTP");
						}
						catch(err3){
							req = false;
						}
					}
				}
				return req;
			}
			
			var http = getXMLHTTPRequest();
			
			function getServerText(){
				var myurl = "";
				http.open("GET", myurl, true);
				http.onreadystatechange = useHttpResponse;
				http.send(null);				
			}
			
			function useHttpResponse(){
				if( http.readyState == 4){
					var mytext = http.responseText;
					alert(mytext);
				}else{
					document.getElementById('test').innerHTML = 'Not fetched';
				}
			}
			
		</script>
	</head>
	<body onload="getServerText()">
		<div id='test'></div>		
	</body>
</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?

Member Avatar for Member #210412

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.

I've written a simple html page called 'test.html' and it works!

<html>
	<head>
		<title>HelloWorld</title>
	</head>
	<body>
		<div id='hello'>Hello</div>		
	</body>		
</html>

All the html codes displayed correctly.
I just wondering why I cannot do this when I linked to some other sites outside.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.