XMLHttpRequest cross domain

43,982

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.

Share:
43,982
tomsk
Author by

tomsk

Updated on July 09, 2022

Comments

  • tomsk
    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
    tomsk about 10 years
    yes thank you, but i dont have access to datafile.php :/
  • Pruthvi Raj Nadimpalli
    Pruthvi Raj Nadimpalli about 10 years
    This it's impossible :( The cross domain target file(server) should allow requests from your domain.
  • tomsk
    tomsk about 10 years
    it musts something exists that allow to display text from external page
  • Pruthvi Raj Nadimpalli
    Pruthvi Raj Nadimpalli about 10 years
    You 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
    tomsk about 10 years
    oh okey, can you send me please any easy tutorial for getting text/html from external page? Thank you very much
  • Pruthvi Raj Nadimpalli
    Pruthvi Raj Nadimpalli about 10 years
    @tomsk Does this answer your question?
  • Sileria
    Sileria over 8 years
    This is what I added and works great: header("access-control-allow-origin: *");