开发者

Jquery Cross-domain Problem when try a put reques in

开发者 https://www.devze.com 2023-04-05 18:59 出处:网络
I try to make a PUT request on JQUERY to a RESTFULL service, when try make the request to url with localhost (http://localhost/Domain) the request work. But when change the url to some ip (http://192.

I try to make a PUT request on JQUERY to a RESTFULL service, when try make the request to url with localhost (http://localhost/Domain) the request work. But when change the url to some ip (http://192.123.32.3) the operation on the server don't fire.

$.ajax({
    type: "PUT",
    url: urlOperation,
    dataType: "json",
    contentType: "application/json",
    data: $.toJSON(submitVote),        success: function (result) 
    {
      alert('Great ...');

    }
});

The error on chrome are 'Method PUT is n开发者_运维技巧ot allowed by Access-Control-Allow-Methods'

I've try solve this adding the put permission on Application_beginRequest event something like that :

private void EnableCrossDmainAjaxCall()
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        } 

After read the jquery.Ajax documention I've try added the property crossDomain='true' without sucess.

Thanks and Regards


The browser will do everything it can to block cross-domain requests. You can use an iframe for ajax requests or you could also use the server the page is running on to proxy the request for you.

Hope that helps, maybe you can check how jQuery handles crossDomain='true', if there isn't an iframe involved it just won't work on all browsers.

0

精彩评论

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

关注公众号