错误“Access-Control-Allow-Origin 不允许 Origin null”使用 JQuery 更新 OData 时
我在本地创建了一个数据库,并使用 Microsoft 的 WCF 数据服务创建了 OData 服务。我已经设法弄清楚如何读取数据,但在尝试更新时,Google Chrome 给出了此错误:
“Access-Control-Allow-Origin 不允许 Origin null。”
仅当我直接从 C 驱动器(没有网络服务器)打开 HTML 页面时才会发生这种情况。如果我通过我的网络服务器,那么它就可以工作。关于如何在不使用网络服务器的情况下使其工作的任何想法?
这是我的代码:
var results=BOData.StephenBO1;
results[0].txtLastStage = $("#txtLastStage").val();
results[0].txtTeamCode = $("#txtTeamCode").val();
results[0].txtClientName = $("#txtClientName").val();
var url = "http://localhost/odata/StephenService.svc/CL_Darwin1('0900000000000000000000000000276')";
var json = JSON.stringify(results[0]);
$.ajax({
url: url,
data: json,
type: "PUT",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert("Saved StephenBO1");
},
error: function (result) {
alert("Update Failure - Status Code=" +
result.status + ", Status=" + result.statusText);
}
});
任何明智和明智的评论将不胜感激...如果您需要任何其他信息,请告诉我。
谢谢, 斯蒂芬
I've created a database locally, and used Microsoft's WCF Data Services to create an OData service. I've managed to figure out how to read the data, but when attempting to update, Google Chrome gives this error:
"Origin null is not allowed by Access-Control-Allow-Origin."
This only happens when I open my HTML page directly from my C drive (without a web server). If I go via my web server, then it works. Any ideas as to how I can get this to work without using a web server?
Here's my code:
var results=BOData.StephenBO1;
results[0].txtLastStage = $("#txtLastStage").val();
results[0].txtTeamCode = $("#txtTeamCode").val();
results[0].txtClientName = $("#txtClientName").val();
var url = "http://localhost/odata/StephenService.svc/CL_Darwin1('0900000000000000000000000000276')";
var json = JSON.stringify(results[0]);
$.ajax({
url: url,
data: json,
type: "PUT",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert("Saved StephenBO1");
},
error: function (result) {
alert("Update Failure - Status Code=" +
result.status + ", Status=" + result.statusText);
}
});
Any wise and intelligent comments would be appreciated... and let me know if you need any additional info.
Thanks,
Stephen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,这个SO问题(和答案)可以解决您的问题。
In my opinion this SO Question (and answer) can resolve your problem.
来自 http://datajs.codeplex.com/documentation:
浏览器有一个策略(通常称为阻止跨域边界的请求的同源策略,如果网页由域提供服务且目标 OData 端点位于不同的域中,则无法执行更新操作。 的假设下设计的:
让 Web 服务器提供中继机制以将请求重定向到适当的 OData 端点。
浏览器,但它通常是在默认情况下打开的。 datajs 是在这样 浏览器都提供此选项。
并非所有
首次使用时提示同意,并且通常会提供较差的用户体验。
From http://datajs.codeplex.com/documentation:
Browsers have a policy (commonly referred to as the same origin policy. that blocks requests across domain boundaries. Because of this restriction update operations cannot be performed if the web page is served by a domain and the target OData endpoint is in a different one. Users have the ability to disable this policy in the browser, however it is typically turned on by default. datajs is designed with such an assumption. The following options are available to support this scenario:
Have the web server provide a relaying mechanism to redirect requests to the appropriate OData endpoint.
Use an XDomainRequest object. This option is not available in all browsers.
Use cross origin XMLHttpRequest object. This option is also not available in all browsers.
Prompt for consent on first use. This option is not available in all browsers and generally provides a poor user experience.