开发者

Calling cross-domain .net-method from jquery in ie/firefox/chrome

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("p开发者_开发百科ost", '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.


Some browsers do not allow cross-domain Ajax calls (a call using the XmlHttpRequest object) for some security reasons.

But the solution is instead of ajax calls use JSONP calls. The JSONP avoids this by making a request suitable for a script file. By using JSONP the following things happen to make cross-domain request possible,

1.Instead of accessing the XHR object, the browser first creates a new script tag to inject into the HTML DOM.

2.The script tag's URL is set to the URL you're looking to get/post(using HTTP GET) data to.

3.The script tag is injected into the page, causing...

4.The request is sent to the server, even if it's cross-domain

5.The server returns the data in the form of a JavaScript function call

6.The browser receives the data and executes the function call

See the urls below to get the implementation details,

http://www.codeproject.com/Articles/78757/Making-Cross-Domain-jQuery-AJAX-Calls.aspx

http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide

Hope this definitely helps you...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜