【问题标题】:Access to XMLHttpRequest at from origin从源访问 XMLHttpRequest
【发布时间】:2019-05-01 08:45:18
【问题描述】:

我有一个 Angular 应用程序,客户端发布功能无法调用服务器 Api 发布方法。 控制台上显示以下错误

":64736/api/Tool/SaveBookMark:1 加载资源失败:服务器响应状态为 405 (Method Not Allowed) :4200/#/envision/maps/google:1 从源“http://localhost:4200”访问“http://localhost:64736/api/Tool/SaveBookMark”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:未通过具有 HTTP ok 状态。”

客户端:角度 5

    const endpoint = this._api.saveBookMark;
let body = JSON.stringify(bookmarkModel);
return this.http.post(endpoint, body, this._api.getJsonOptions())
    .map(this._api.extractData)
    .catch(this._api.handleError);

服务器端:Mvc Api

[HttpPost]
    public string SaveBookMark(BookmarkModel bookmarkModel)
    {
        string res = string.Empty;
        try
        {

        }
        catch (Exception ex)
        {
        }
        return res;
    }

【问题讨论】:

    标签: angular api model-view-controller http-post same-origin-policy


    【解决方案1】:

    在您的 WebApiConfig.cs 文件中添加:

      public static void Register(HttpConfiguration config)
      {
         config.EnableCors();
      }
    

    【讨论】:

      猜你喜欢
      • 2019-04-28
      • 1970-01-01
      • 2016-12-05
      • 2021-03-08
      • 2021-01-14
      • 2022-01-16
      • 2021-10-23
      • 2021-03-27
      • 2023-03-09
      相关资源
      最近更新 更多