简体   繁体   中英

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. 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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM