Search code examples
oktaokta-api

Okta AJAX Post Error - No 'Access-Control-Allow-Origin' header


I am attempting to create a session token via the okta api. I have enabled CORS via the okta documentation.

The "GET" below works:

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);
    });
}

But the "POST" below fails with the error:

XMLHttpRequest cannot load https://mydomain.okta.com/api/v1/sessions?additionalFields=cookieToken. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mydomain' is therefore not allowed access.

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);
        }
    });
}

Solution

  • The Create Session API call is not CORS enabled.