我正在尝试通过 okta api 创建会话令牌。我通过 okta 文档启用了 CORS。
下面的“GET”有效:
function testGetApps() {
var baseUrl = 'https://mydomain.okta.com';
$.ajax({
url: baseUrl + '/api/v1/users/userId/appLinks',
type: 'GET',
accept: 'application/json',
headers: { Authorization : 'SSWS oktaApiKey'}
}).done(function (data) {
console.log(data);
})
.fail(function (xhr, textStatus, error) {
var title, message;
switch (xhr.status) {
case 403:
title = xhr.responseJSON.errorSummary;
message = 'Please login to your Okta organization before running the test';
break;
default:
title = 'Invalid URL or Cross-Origin Request Blocked';
message = 'You must explictly add this site (' + window.location.origin + ') to the list of allowed websites in your Okta Admin Dashboard';
break;
}
alert(title + ': ' + message);
});
}
但是下面的“POST”失败并出现错误:
XMLHttpRequest 无法加载https://mydomain.okta.com/api/v1/sessions?additionalFields=cookieToken。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问源“ http://mydomain ”。
function initOktaSession() {
var txtUserName = $('UserName').val();
var txtPassword = $('Password').val();
var data = { username: txtUserName, password: txtPassword };
var requestUrl = 'https://mydomain.okta.com/api/v1/sessions?additionalFields=cookieToken';
//Call Webservices to update
$.ajax({
type: 'POST',
url: requestUrl,
headers: {authorization : 'SSWS oktaApiKey'},
contentType: 'application/json',
data: JSON.stringify(data),
async: true,
success: function (data) {
console.log(data);
},
error: function (err) {
console && console.log(err);
!console && alert(err.status + ' ' + err.statusText);
}
});
}