0

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

Stephen Oberauer
  • 5,237
  • 6
  • 53
  • 75
  • Is there a reason why you cannot point a web server to your development files? – Jeremy May 17 '11 at 14:11
  • Yes, the above JavaScript gets generated by the program I'm writing. The idea is so that the HTML files containing the script can run from anywhere. – Stephen Oberauer May 17 '11 at 14:25

2 Answers2

0

In my opinion this SO Question (and answer) can resolve your problem.

Community
  • 1
  • 1
pmaruszczyk
  • 2,157
  • 2
  • 24
  • 49
0

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.

Stephen Oberauer
  • 5,237
  • 6
  • 53
  • 75