I’m trying to use the Users API to search for users. The problem is that i’m keep getting CORS errors, so went a step back and toke the example code of the Cors page of Okta https://developer.okta.com/docs/api/getting_started/enabling_cors
There is some example code
var baseUrl = 'https://{yourOktaDomain}';
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.onerror = function() {
alert('Invalid URL or Cross-Origin Request Blocked. You must explicitly add this site (' + window.location.origin + ') to the list of allowed websites in the administrator UI');
}
xhr.onload = function() {
alert(this.responseText);
};
xhr.open('GET', baseUrl + '/api/v1/users/me', true);
xhr.withCredentials = true;
xhr.send();
} else {
alert("CORS is not supported for this browser!")
}
When using this, i’m getting back a result.
When changing it to the code below there will be a CORS error. The only line changed is xhr.open(‘GET’, baseUrl + ‘/api/v1/users/me’, true); to xhr.open(‘GET’, baseUrl + ‘/api/v1/users’, true);
var baseUrl = 'https://{yourOktaDomain}';
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.onerror = function() {
alert('Invalid URL or Cross-Origin Request Blocked. You must explicitly add this site (' + window.location.origin + ') to the list of allowed websites in the administrator UI');
}
xhr.onload = function() {
alert(this.responseText);
};
xhr.open('GET', baseUrl + '/api/v1/users', true);
xhr.withCredentials = true;
xhr.send();
} else {
alert("CORS is not supported for this browser!")
}
The error is Failed to load https://{yourOktaDomain}/api/v1/users: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://test.com’ is therefore not allowed access.