0

我尝试在 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' 但没有成功。

谢谢并恭祝安康

4

1 回答 1

1

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

希望对您有所帮助,也许您可​​以检查 jQuery 如何处理 crossDomain='true',如果不涉及 iframe,它就不适用于所有浏览器。

于 2011-09-16T03:02:51.907 回答