简体   繁体   English

使用JQuery更新OData时出现错误“ Access-Control-Allow-Origin不允许Origin null”

[英]Error “Origin null is not allowed by Access-Control-Allow-Origin” when using JQuery to update OData

I've created a database locally, and used Microsoft's WCF Data Services to create an OData service. 我已经在本地创建了一个数据库,并使用了Microsoft的WCF数据服务来创建OData服务。 I've managed to figure out how to read the data, but when attempting to update, Google Chrome gives this error: 我设法弄清楚了如何读取数据,但是在尝试更新时,Google Chrome出现此错误:

"Origin null is not allowed by Access-Control-Allow-Origin." “ Access-Control-Allow-Origin不允许使用原始null。”

This only happens when I open my HTML page directly from my C drive (without a web server). 仅当我直接从C驱动器(没有Web服务器)打开HTML页面时,才会发生这种情况。 If I go via my web server, then it works. 如果我通过我的Web服务器,那么它可以工作。 Any ideas as to how I can get this to work without using a web server? 关于如何在不使用Web服务器的情况下使它工作的任何想法?

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 谢谢斯蒂芬

我认为这个SO问题(和答案)可以解决您的问题。

From http://datajs.codeplex.com/documentation : 来自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: 浏览器具有一项策略(通常称为同一原始策略。该策略阻止跨域边界的请求。由于此限制,如果网页由域提供服务且目标OData端点位于另一个域中,则无法执行更新操作。用户可以在浏览器中禁用此策略,但是默认情况下通常会启用此策略。datajs的设计是基于这种假设的,以下选项可用于支持此方案:

Have the web server provide a relaying mechanism to redirect requests to the appropriate OData endpoint. 让Web服务器提供一种中继机制,以将请求重定向到适当的OData端点。

Use an XDomainRequest object. 使用XDomainRequest对象。 This option is not available in all browsers. 此选项并非在所有浏览器中都可用。

Use cross origin XMLHttpRequest object. 使用跨源XMLHttpRequest对象。 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. 此选项并非在所有浏览器中都可用,并且通常会提供较差的用户体验。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Access-Control-Allow-Origin不允许使用Origin null(同步,无jQuery) - Origin null is not allowed by Access-Control-Allow-Origin (Synchronous, no jQuery) jQuery使用WCF。 错误:XMLHttpRequest无法加载Access-Control-Allow-Origin不允许使用Origin null - Jquery to consume WCF. error: XMLHttpRequest cannot load Origin null is not allowed by Access-Control-Allow-Origin 如何使用Webworks修复Javascript(jquery)中的“ Access-Control-Allow-Origin不允许访问null的原始null”错误? - How do i fix “Origin null is not allowed by Access-Control-Allow-Origin” error in Javascript (jquery) with webworks? Access-Control-Allow-Origin不允许使用Chrome Origin null - Chrome Origin null is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许使用null - Origin null is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin 不允许来源 null - Origin null is not allowed by Access-Control-Allow-Origin 无法与jsonp配合使用的Access-Control-Allow-Origin不允许使用Origin null - Origin null is not allowed by Access-Control-Allow-Origin not working with jsonp Access-Control-Allow-Origin不允许使用原点null - Origin null is not allowed by Access-Control-Allow-Origin Jquery (jfeed) - Access-Control-Allow-Origin 不允许来源 xxxxx - Jquery (jfeed) - Origin xxxxx is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin 不允许 Origin - Origin is not allowed by Access-Control-Allow-Origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM