Salut,
J'ai bien vu qu'il y avait eu des échanges sur le sujet (j'ai d'ailleurs suivi les conseils) et malgrès ça, j'ai toujours l'erreur :
Access to XMLHttpRequest at aaaa from origin bbbb has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
Lorsque j'essaye d'accéder à mon api depuis mon client angular.
Voici mon code angular :
environment.ts
Code TypeScript : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 export const environment = { production: false, language:"fr-fr", BBApiServiceEndPoint: "http://BB2/api/", httpOptions : { headers: new HttpHeaders({ 'Access-Control-Allow-Origin':'http://BB2/*', 'Access-Control-Allow-Headers' : 'X-Requested-With, Content-Type', 'Access-Control-Allow-Methods' : 'POST, GET, OPTION', 'Content-Type': 'application/json' }) } };
BBApiService.ts
Code TypeScript : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 getAllTests(): Observable<any> { var endPointUrl : string = (new ApiHelperService()).buildBBUri("bbAllTests"); var data:any; return this.http .get<BbTest>(endPointUrl,environment.httpOptions); } }
Côté .net :
WebApiConfig.cs
Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services config.EnableCors(); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{language}/{controller}/{id}", defaults: new { language = PortalEnvironment.DefaultLanguage, id = RouteParameter.Optional } ); config.Formatters.Remove(config.Formatters.XmlFormatter); } }
dans le web.config
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> </httpProtocol> </system.webServer>
Dans ma web API:
Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 [HttpGet] [Route("api/{language}/bbAllTests")] [EnableCors("*", "*", "*")] public string Profiles() { string toReturn = ""; try { Assembly currentAssembly = Assembly.GetAssembly(this.GetType()); var profiles = CommonContractsAndTools.ConfigTools.GetResourceFileText(currentAssembly, "profiles." + PortalEnvironment.Env + ".config"); var xDocument = XDocument.Parse(profiles); var sb = new StringBuilder(); JsonSerializer.Create().Serialize(new CustomJsonWriter(new StringWriter(sb)), xDocument.FirstNode); toReturn = sb.ToString(); BBLogger.Info("Profiles successfully found"); } catch (Exception ex) { BBLogger.Error(ex); } return toReturn; }
Je ne comprends pas pourquoi ça ne fonctionne pas.
Ne m'envoyez pas dans google j'ai lu plusieurs pages qui me disent de faire ce que j'ai fait, il doit manquer un truc quelque part, je ne trouve pas.
D'avance merci pour votre aide
Partager