10
Cross Site XMLHttpRequest from JavaScript can it be done?
I understand the limitations and why it is not generally able to work, but as of firefox 3.5 there is the
Access-Control-Allow-Origin: *
which is supposed to allow this to work.
It tells the browser that the server does not care if the request is sent to it from a domain that did not serve the page.
The code I am using is below.
function sendData(webservicePayload, callbackFunction) {
var request = null;
if (!window.XMLHttpRequest) { // code for IE
try {
request = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
request = new ActiveXObject('Microsoft.XMLHTTP');
} catch (E) {
return 'Create XMLHTTP request IE';
}
}
} else { // code for Mozilla, etc.
request = new XMLHttpRequest();
}
/*
* Setup the callback function
*/
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status < 300) {
eval(callbackFunction);
}
};
if (!request) {
nlapiLogExecution('ERROR', 'Create XMLHTTP request', 'Failed');
return;
}
/*
* Setup the request headers
*/
request.open('POST','http://www.another.domain.co.uk/webservice.asmx', true);
request.setRequestHeader('Man','POST http://www.another.domain.co.uk/webservice.asmx HTTP/1.1');
request.setRequestHeader('MessageType', 'CALL');
request.setRequestHeader('Content-Type', 'text/xml; charset="utf-8"');
request.setRequestHeader('Cache-Control', 'no-cache');
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setRequestHeader('SOAPAction','http://www.another.domain.co.uk/WebService/eService');
request.send(webservicePayload);
}
This is sending the correct request header
REQUEST
OPTIONS /webservice.asmx HTTP/1.1
Hos