Question

I have a page which calls an XMLHttpRequest to a page from another site. The problem is, the status returned is 0. The code of the caller page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Generator" content="Dev-PHP 2.6.0" />
<meta name="Keywords" content="your,keywords,here" />
<meta name="Description" content="." />

</head>
<body>
<p>
  <input type="text" name="txtReturn" id="txtReturn" size=75 />
</p>

<textarea name="taStatus" id="taStatus" cols="75" rows="25"></textarea>

<p>&nbsp; </p>
<script type="text/javascript">
function getReturn()
{
    var txtBox = document.getElementById("txtReturn");
    var taStatus = document.getElementById("taStatus");

    txtBox.value = "Preparing to get Response";

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        // alert('IE7+, Firefox, Chrome, Opera, Safari');
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        // alert('IE6, IE5');
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {   
        taStatus.innerHTML = taStatus.innerHTML + "URL: " + url + ".\nReadyStatus: " + xmlhttp.readyState + ".\nStatus: " + xmlhttp.status + ".\nStatusText: " + xmlhttp.statusText + ".\nresponseText: " + xmlhttp.responseText + ".\n\n";

        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
                // alert(xmlhttp.responseText);
            txtBox.value = xmlhttp.responseText;
                // alert(xmlhttp.responseText);
        }

    }

    var url = 'http://www.ip.com.om/site/checkdata.php';
    var url2 = 'http://badihbarakat.com/site/checkdata.php';
    var url3 = '../site/checkdata.php';

    xmlhttp.withCredentials = true;
    // url = url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime();
    xmlhttp.open("GET", url, true);
    xmlhttp.send();

}

    getReturn();
</script>
</body>
</html>

The called .php page on the other site, just echo a string with the date and time.

Can anyone advise what can be the problem? I am using FireFox 27 and I have checked the Error Browser and it is showing:

**GET http://www.ip.com.om/site/checkdata.php [HTTP/1.1 200 OK 710ms]**

and when you click it, the details of the request shows:

Request URL:    http://www.ip.com.om/site/checkdata.php
Request Method:     GET
Status Code:    HTTP/1.1 200 OK

**Request Headers 17:27:37.000**
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0
Referer:    http://badihbarakat.com/local/
Origin: http://badihbarakat.com
Host:   www.ip.com.om
Connection: keep-alive
Accept-Language:    en-US,en;q=0.5
Accept-Encoding:    gzip, deflate
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

**Response Headers Δ710ms**
X-Powered-By:   PHP/5.3.26
X-CFLO-Cache-Result:    TCP_REFRESH_MISS
Server: Apache
Date:   Sat, 01 Mar 2014 13:27:42 GMT
Content-Type:   text/html
Content-Length: 37
Connection: Keep-Alive
Age:    0

Awaiting your valuable replies...

Badih Barakat

Was it helpful?

Solution

Due to security issues,you can't do xmlhttp requests to a server in a different domain. You could either consume the service from server side code and then possible proxy it to the page via your own service. check the following post. Still confused about using XMLHTTPRequest cross domain

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top