Skip to main content
Question

Authentication error from API using ajax query

  • November 14, 2016
  • 1 reply
  • 0 views

Forum|alt.badge.img+6

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

Forum|alt.badge.img+7
  • Contributor
  • 31 replies
  • December 15, 2016

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)


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings