Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 6:400调用API url获取身份验证令牌时请求错误_Angular_Typescript_Asp.net Web Api_Bearer Token_Bad Request - Fatal编程技术网

Angular 6:400调用API url获取身份验证令牌时请求错误

Angular 6:400调用API url获取身份验证令牌时请求错误,angular,typescript,asp.net-web-api,bearer-token,bad-request,Angular,Typescript,Asp.net Web Api,Bearer Token,Bad Request,在Angular 6应用程序中,我在调用登录令牌的API url时遇到Http 400错误请求错误 如果我从POSTMAN调用相同的url,那么API工作正常 但是当从角度应用程序调用时给出一个错误 Service.ts(角度) web.config <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" />

在Angular 6应用程序中,我在调用登录令牌的API url时遇到Http 400错误请求错误

如果我从POSTMAN调用相同的url,那么API工作正常

但是当从角度应用程序调用时给出一个错误

Service.ts(角度)

web.config

<httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="*" />
        <add name="Access-Control-Allow-Methods" value="*" />
      </customHeaders>
    </httpProtocol>
有人能帮我解决这个问题吗。

首先看这篇文章:

然后,如果您需要查看此页面:

关于在ASP.NET Web API 2中启用跨源请求的快速指南,分三步进行:

请执行以下操作: 1.首次安装

Install-Package Microsoft.AspNet.WebApi.Cors
2.打开文件App_Start/WebApiConfig.cs。将以下代码添加到WebApiConfig.Register方法:

using System.Web.Http;
namespace WebService
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // New code
            config.EnableCors();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
三,。接下来,将
[EnableCors]
属性添加到
TestController
类:

using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace WebService.Controllers
{
    [EnableCors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")]
    public class TestController : ApiController
    {
        // Controller methods not shown...
    }
}
首先看这篇文章:

然后,如果您需要查看此页面:

关于在ASP.NET Web API 2中启用跨源请求的快速指南,分三步进行:

请执行以下操作: 1.首次安装

Install-Package Microsoft.AspNet.WebApi.Cors
2.打开文件App_Start/WebApiConfig.cs。将以下代码添加到WebApiConfig.Register方法:

using System.Web.Http;
namespace WebService
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // New code
            config.EnableCors();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
三,。接下来,将
[EnableCors]
属性添加到
TestController
类:

using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace WebService.Controllers
{
    [EnableCors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")]
    public class TestController : ApiController
    {
        // Controller methods not shown...
    }
}

请您向我们展示webapi控制器并启动升级,请查看更新的post@MasoudBimar。请向我们展示webapi控制器并启动升级,请查看更新的post@MasoudBimar