在尝试put请求时出现Jquery跨域问题

时间:2011-09-16 01:37:22

标签: jquery ajax cross-domain-policy

我尝试在JQUERY上向RESTFULL服务发出PUT请求,当尝试使用localhost(http:// localhost / Domain)请求url时请求工作。但是当将url更改为某个ip(http://192.123.32.3)时,服务器上的操作不会触发。

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

    }
});

chrome上的错误是'Access-Control-Allow-Methods'不允许使用方法PUT

我尝试解决这个问题,在Application_beginRequest事件中添加put权限:

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();
            }
        } 

在阅读了jquery.Ajax文档之后,我尝试添加了属性crossDomain ='true'而没有成功。

谢谢和问候

1 个答案:

答案 0 :(得分:1)

浏览器将尽其所能阻止跨域请求。您可以将iframe用于ajax请求,也可以使用运行该页面的服务器来代理请求。

希望有所帮助,也许你可以查看jQuery如何处理crossDomain ='true',如果没有涉及iframe,它将无法在所有浏览器上运行。

相关问题