Author Topic: JavaScript API Example - Authentication  (Read 771 times)

Offline matt.bull

  • Newbie
  • *
  • Posts: 1
  • Karma: 0
    • View Profile
JavaScript API Example - Authentication
« on: January 04, 2016, 06:30:48 PM »
I have been working on a JavaScript HTML5 example and not getting very far. I get the error

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

I am adding this to the client, however I believe it is the server that needs to provide this access. Does anyone have any JavaScript examples for authentication?

My Script Below

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="text/javascript">
            (function () {

    'use strict';

    var httpReq = new XMLHttpRequest();
    var url = 'https://myserver/ContentManager/api/rest/auth/login';
    var data = {username: 'username', password: 'password'};

    httpReq.open('POST', url, true);
    console.log('good');

    httpReq.setRequestHeader('Access-Control-Allow-Headers', '*');
    httpReq.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
    httpReq.setRequestHeader('Access-Control-Allow-Origin', '*');
    console.log('ok');

    httpReq.onreadystatechange = function () {
        console.log('test');
        if (httpReq.readyState === 4 && httpReq.status === 'success') {
            console.log('test');
            alert(httpReq.responseText);
        }
    };

    httpReq.send(data);
                alert(httpReq.responseText);

}());
</script>
    </head>
    <body>
        <div>TODO write content</div>
    </body>
</html>
« Last Edit: January 04, 2016, 06:33:46 PM by matt.bull »

Offline jamesnotjim

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Re: JavaScript API Example - Authentication
« Reply #1 on: September 19, 2017, 11:09:35 AM »
I'm going down the same road as well, and with similar results. Did you ever get your JavaScript-based version to authenticate?