这与https://stackoverflow.com/a/10143166/2360569有关,但我还没有得到公认的工作答案。
我只是从客户端脚本发出 ajax 请求,该脚本在本地工作,但不在测试服务器上。
ClientScript.js:
var AppViewModel = function () {
var self = this;
self.Refresh = function () {
$.ajax({
url: "http://localhost/AppName/ControllerName/GetData",
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function (data, textStatus, jqXHR) {
//do stuff
},
error: function (jqXHR, textStatus, errorThrown) {
//should do something here
},
complete: function (jqXHR, textStatus) {
//other stuff
}
});
}
对于我的 MVC Web 服务,我将此函数添加到 global.asax.cs:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://testdev");
}
当我部署到“testdev”服务器时,我仍然收到此错误消息:
XMLHttpRequest cannot load http://localhost/AppName/ControllerName/GetData.
Origin http://testdev is not allowed by Access-Control-Allow-Origin.
我在这里想念什么?