okta Angular 7 App returning a CORS error

允我心安 提交于 2019-12-07 06:56:28

All you would need to do is send 'Access-Control-Allow-Origin':'*' along with the request in your angular service file. I have shown below a way to do this. This simply modifies the server to add the header Access-Control-Allow-Origin: * to enable cross-origin requests from anywhere.

import { HttpClient, HttpHeaders, HttpClientModule } from '@angular/common/http';

const httpOptions = {
  headers: new HttpHeaders({ 
    'Access-Control-Allow-Origin':'*',
    'Authorization':'authkey',
    'userid':'1'
  })
};

public baseurl = 'http://localhost/XXXXXX';

userAPI(data): Observable<any> {
  return this.http.post(this.baseurl, data, httpOptions)**// Pass in the options in the request**
    .pipe(
      tap((result) => console.log('result-->',result)),
      catchError(this.handleError('error', []))
    );
}

Hope this helps.

From the CORS definition in Mozilla.

A web application executes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, and port) than its own origin.

So for your request to work, you have two options:

  1. Add an 'Access-Control-Allow-Origin' header to your API's response with a value of your ip ie. Access-Control-Allow-Origin: "172.168.1.1"
  2. Add your external IP Address to the Allowed addresses via the Okta interface if you have access to it. Check here.

You can view your external ip address via website that let you view them. Try to check yours at Cmyip.

https://dev-979343.oktapreview.com/oauth2/default/.well-known/openid-configuration

is a redirect URL so you gotta redirect, don't use XMLHttpRequest. You can test this by simply pasting the above URL to the browser. As you can see, it gives you the data you looking for.

With the help of @MattRaible pointing me to the right direction I was able to resolve this issue. Basically, I didn't have authority to add a Trusted Origin (menu item API | Trusted Origin) on the okta site. Once an administrator added ‘http://localhost:4200’ as a Trusted Origin everything started working.

Thanks to everyone who commented on this post.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!