I have been trying to call a .NET method (both as asmx file and as a normal aspx file) from another domain through JQuery and I just can't get the job done in every browser. At the moment it works fine in Firefox but not in IE.
function save() {
if (jQuery.browser.msie && window.XDomainRequest) {
// Use XDR
var params = "{'height':" + 10 + ",'width':" + 10 + ",'pos':'" + 10 + "'}";
var xdr = new XDomainRequest();
xdr.onerror = alert_error;
xdr.ontimeout = alert_timeout;
xdr.onprogress = alert_progress;
xdr.onload = alert_loaded;
xdr.timeout = 10000;
xdr.open("post", 'http://domain/reciever.asmx/setdata');
//Tried as webservice and as a normal aspx page
//xdr.open("post", 'http://domain/default.aspx');
xdr.send(params);
}
else {
var params = "pos=" + positions + "&width=" + screenWidth + "&height=" + screenHeight;
var myAjax = new jQuery.ajax(
"http://domain/default.aspx",
{
type: 'post',
cache: false,
crossDomain: true,
data: params
});
}
}
On the server end the web.config has:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
And the webservice
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string setdata(int width, int height, string pos)
The aspx page returns:
Response.Clear();
Response.ContentType = "text/plain";
Response.AddHeader("Access-Control-Allow-Origin", "*");
Response.End();
Fiddler says: Fiddler has detected a protocol violation in session #2565. Content-Length mismatch: Request Header indicated 38 bytes, but client sent 0 bytes. So i would believe it to be the "Access-Control-Allow-Origin" but this I have set (to my knowledge at least).
Can someone help me understand what I am doing wrong.