XMLHttpRequest cross domain
Solution 1
If Access-Control-Allow-Origin
header is set in response headers of datafile.php
it works :)
Solution 2
you can send the request back to your server, then redirect it to wherever you want:
javascript function:
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();}
else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;}}
xmlhttp.open("POST","response.php",true);
xmlhttp.send();
.htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^response(.*?)\.php http://example.com/query [R]
The javascript function will write the response from example.com in the txtHint div.
I wrote it like this because this is how I use it in my app so I only did minor changes. Hope it helps
Solution 3
You should be using postMessage to get around the cross domain restrictions.
tomsk
Updated on July 09, 2022Comments
-
tomsk almost 2 years
i have this javascript code for extract text from page, it works fine if i open file in my domain, but i cant get text from file in another domain, because some security reasons. So my question is how can i please extract text from another website in javascript, please without jquery.
Thank you
function reqListener () { console.log(this.responseText); } var xhr = new XMLHttpRequest(); xhr.onload = reqListener; xhr.onreadystatechange = function() { if (xhr.readyState == 4) { alert(xhr.responseText); } } xhr.open('GET', 'http://anotherweb.com/datafile.php', true); xhr.setRequestHeader('Content-Type', 'text/plain'); xhr.send(null);
I tried this and it doesnt work.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> $(document).ready(function(){ $("button").click(function(){ $.ajax({ url: "http://localhost/index.php", dataType : "json", contentType: "application/json; charset=utf-8", cache: false, success: function(response) { alert(response); }, error: function (e) { } }); }); }); </script> </head> <body> <button>Send an HTTP GET request to a page and get the result back</button> </body> </html>
-
tomsk about 10 yearsyes thank you, but i dont have access to datafile.php :/
-
Pruthvi Raj Nadimpalli about 10 yearsThis it's impossible :( The cross domain target file(server) should allow requests from your domain.
-
tomsk about 10 yearsit musts something exists that allow to display text from external page
-
Pruthvi Raj Nadimpalli about 10 yearsYou cannot do a cross domain get of text/html in any modern browser. The only return type that I know of that works currently is jsonp
-
tomsk about 10 yearsoh okey, can you send me please any easy tutorial for getting text/html from external page? Thank you very much
-
Pruthvi Raj Nadimpalli about 10 years@tomsk Does this answer your question?
-
Sileria over 8 yearsThis is what I added and works great: header("access-control-allow-origin: *");