My code looks like this, which is recommended for IE to work, but it only works in Chrome and FF. Is there a correct way to access a url from another domain. Furthermore, the domain is a domain I own and can allow access to the scripts trying to access it:
<script language="javascript" type="text/javascript">
function sendRequest(url,callback,postData) {
var req = createXMLHTTPObject();
if (!req) return;
var method = (postData) ? "POST" : "GET";
req.open(method,url,true);
req.setRequestHeader('User-Agent','XMLHTTP/1.0');
if (postData)
req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
req.onreadystatechange = function () {
if (req.readyState != 4) return;
if (req.status != 200 && req.status != 304) {
// alert('HTTP error ' + req.status);
return;
}
callback(req);
}
if (req.readyState == 4) return;
req.send(postData);
}
var XMLHttpFactories = [
function () {return new XMLHttpRequest()},
function () {return new ActiveXObject("Msxml2.XMLHTTP")},
function () {return new ActiveXObject("Msxml3.XMLHTTP")},
function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];
function createXMLHTTPObject() {
var xmlhttp = false;
for (var i=0;i<XMLHttpFactories.length;i++) {
try {
xmlhttp = XMLHttpFactories[i]();
}
catch (e) {
continue;
}
break;
}
return xmlhttp;
}
function handleRequest(req) {
var MyResponse = req.responseText;
document.open();
document.write(MyResponse);
document.close();
}
sendRequest("http://anotherdomain.com/urlwithcontentneeded.php",handleRequest);
</script>
IE does not suppoort cross domain requests in this way but does have a way using the XDomainRequest object instead, see http://msdn.microsoft.com/en-us/library/cc288060(v=vs.85).aspx
It works in much the same way though, and yes it's a pain there are two ways to do it in different browsers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With