我试图在一个名为YDN的框架中访问我的WebApi服务。它是一个本地存储类型数据库。它允许您从URL加载,我正在尝试使用交叉原产地调用。
这是创建XMLHttpRequest并进行调用的代码:
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
var me = this;
xhr.onload = function(e) {
var lines = xhr.responseText.split('\n');
var documents = [];
for (var i = 0; i < lines.length; i++) {
var data = lines[i].split(';');
if (data.length == 2) {
for(i=0; i<data.length; i++) {
// Here, I will be loading the records into the YDN-DB.
}
}
}
var msg = Documents.length + ' Documents loaded, indexing...';
me.setStatus(msg);
var i=0;
var load = function(i) {
var n = 50;
me.db.put('Document', docs.slice(i, i + n)).then(function(keys) {
i = i + n;
if (i < docs.length) {
this.setStatus(msg + ' ' + i);
load(i);
} else {
this.setStatus(msg + ' done.');
}
}, function(e) {
throw e;
}, me);
};
};
xhr.send();我不断地犯错误,我不知道为什么。
XMLHttpRequest cannot load http://localhost:51946/api/Archive/c4020343622d57162cbdc11e603a49d93d64018e5c/1026697/1/10000/D.
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8040' is therefore not allowed access.这是我来自控制器的WebAPI函数,我启用了CORS,并从原点:
[HttpGet]
[ResponseType(typeof(List<string>))]
[Route("api/Archive/{LogonTicket}/{PID}/{MinRow}/{MaxRow}/{Direction}")]
[EnableCors("http://localhost:8040", // Origin
"Accept, Origin, Content-Type, Options", // Request headers
"GET", // HTTP methods
PreflightMaxAge = 600 // Preflight cache duration
)]
public object Archive(string LogonTicket, int PID, int MinRow, int MaxRow, string Direction)
{
if (svc.ValidateTicket(LogonTicket, PID))
{
List<string> e = mp.GetArchiveJson(PID.ToString(), MinRow, MaxRow, Direction);
return e;
}
else
{
throw new AuthenticationException("Invalid LogonTicket");
}
}有什么主意吗?我需要向XMLHttpRequest添加第二个调用吗?
发布于 2016-11-01 23:06:59
确保遵循文献资料中有关“启用CORS”的所有步骤。有三件事是必要的:
Install-Package Microsoft.AspNet.WebApi.CorsRegister(HttpConfiguration config),包括对config.EnableCors()的调用。EnableCors属性装饰控制器或方法,例如[EnableCors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")]考虑到EnableCors属性的示例用法,很明显您已经得到了#3,它将#1作为先决条件,所以您也得到了它。看来你错过了2号。
https://stackoverflow.com/questions/40365787
复制相似问题