【问题标题】:Web Api: Access-Control-Allow-Origin Not present errorWeb Api: Access-Control-Allow-Origin Not present 错误
【发布时间】:2015-11-09 22:46:51
【问题描述】:

此代码可以正常工作。

[HttpPost]
[Route("CreateItem/{Id}")]
public IHttpActionResult CreateItem(Int64 Id)
{
     //Do Something

     return Ok();
}

但是,一旦我引入异步调用,我就会收到一条错误消息:

请求中没有“Access-Control-Allow-Origin”标头 资源代码:

[HttpPost]
[Route("CreateItem/{Id}")]
public IHttpActionResult CreateItem(Int64 Id)
{
     //Do Something
     AsyncCall();
     return Ok();
}
async void AsyncCall(){
     //Parallel job to be done. Doesn't matter if this is running and
     CreateItem call returns 200.
}

请注意:没有异步调用的正常控制器操作可以完美运行。但是,一旦从控制器 Action 中进行异步函数调用,我就会收到错误。

我已经通过app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);激活了跨域请求

【问题讨论】:

    标签: c# asynchronous asp.net-web-api cors


    【解决方案1】:

    您应该等待 AsynCall 方法调用。

    【讨论】:

    • 是的,那行得通。但是,异步方法耗时太长,我不希望用户等待。
    猜你喜欢
    • 2018-02-09
    • 1970-01-01
    • 2021-01-07
    • 2018-11-10
    • 1970-01-01
    • 2016-12-09
    • 2018-12-12
    • 1970-01-01
    • 2014-12-25
    相关资源
    最近更新 更多