簡體   English   中英

使用 Angular 的 http 請求 WebApi 失敗

[英]Failed http request WebApi using Angular

我正在嘗試從 angular 連接在 .net 中創建的服務,但是我收到了 CORS 錯誤,我尋找了很多鏈接,但沒有一個對我有幫助,因為大多數解決方案都是針對 .NET CORE 而我使用的是標准 .net 。

從角度調用:

 return this.http.post('http://localhost:50112/api/auth', userdata).toPromise();

接口:

public class AuthController : ApiController
{
    public async Task<IHttpActionResult> Auth(User userdata)
    {
        var result = await bllUser.auth(userdata);
        return Ok(result);
    }
}

和這個錯誤:

Access to XMLHttpRequest at 'http://localhost:50461/api/auth' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我還嘗試在我的 webapiconfig 文件中添加 config.EnableCors 語句,但它告訴我該語句不存在

在此處輸入圖片說明

我很感激你的幫助,沒有解決方案為我服務

使用啟用 cors

將CORS安裝到API項目“Install-Package Microsoft.Asp.Net.WebApi.Cors”中。 並更改您的注冊方法

public static void Register(HttpConfiguration config)
{
    var cors = new EnableCorsAttribute("www.example.com", "*", "*");
    config.EnableCors(cors);
    // ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM