尝试放置请求时的 Jquery 跨域问题
我尝试在 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' 没有成功。
感谢和问候
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 not 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
浏览器会尽一切努力阻止跨域请求。您可以使用 iframe 进行 ajax 请求,也可以使用页面运行的服务器来代理您的请求。
希望有帮助,也许你可以检查 jQuery 如何处理 crossDomain='true',如果没有涉及 iframe,它就无法在所有浏览器上工作。
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.