我尝试在 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' 但没有成功。
谢谢并恭祝安康