Authentication error from API using ajax query

jkeller13
New Contributor III

Does anyone know how the JSS API expects credentials from an ajax query? This is using a read-only account to gather machine information through a SharePoint page. The below code is in-place but every iteration returns an authentication error. (This doesn't include the text field where a user would enter a hostname). I know that the account is correct because I can successfully GET with curl, but not through ajax.

$.ajax({
                    type: "POST",
                    url: apiURL,
                    async: false,
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    processData: false,
               headers: {
                                "Authorization": "Basic " + btoa("username:password")
                                },
                    success: function (){
                                alert('Thanks!');
                    }
                });
1 REPLY 1

GSSRyan
New Contributor III

It appears that you're hashing the string "username:password" rather than a constructing the string from variables... You mentioned having a form that collects the user data, so try:

userVar = document.getElementById('userInputId').value;
pwVar = document.getElementById('pwInputId').value;

then adjust your headers option:

headers: { "Authorization": "Basic " + btoa(userVar+':'+pwVar) }

Also, when doing a POST, the 'data' option is expected and should be in XML format:

data: "<xml>SomeXMLHere</xml>"

Additionally, if the JSS account you're using to authenticate is Read-Only, POST requests won't be available to it. If you're meaning to do a GET request, be sure to change the type option:

type: 'GET'

Finally, be sure that the endpoint URL is set correctly (including the port)