Web Api endpoints used in external javascript application throw 401 error despite of passing credentials in header in JavaScript XmlHttpRequest

Web Api endpoints used in external javascript application throw 401 error despite of passing credentials in header in JavaScript XmlHttpRequest.

The code used was similar to below, which is throwing 401 error in browser.

How to authenticate dynamically to render content in front end javascript application, which is exposed via Appian Web Api.

 

$.ajax({
    xhrFields: {
        withCredentials: true
    },
    headers: {
        'Authorization': 'Basic ' + btoa('myuser:mypswd')
    },
    url: "https://xxxx.appiancloud.com/api/GetImage/1234"
});

  Discussion posts and replies are publicly visible

Parents
  • Hi Anusha,

    Did you add the cross origin of your IP address in the Appian? Please check that one and you can refer following code snippet.
    URL = "xyz.appian.com/.../addOrRemoveUserFromGroup";
    var xmlhttp = new XMLHttpRequest();
    //xmlhttp.onreadystatechange = callbackFunction(xmlhttp);
    xmlhttp.open("POST", URL, false);
    xmlhttp.setRequestHeader("Content-Type", "application/json");
    xmlhttp.setRequestHeader('Authorization', 'Basic '
    + window.btoa('test:test));
    xmlhttp.setRequestHeader('Access-Control-Allow-Origin', '*');
    xmlhttp.setRequestHeader('Access-Control-Allow-Credentials', true);
    xmlhttp.setRequestHeader('Access-Control-Allow-Methods', 'POST');
    xmlhttp
    .setRequestHeader('Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept,X-Csrf-Token,Authorization');
    //xmlhttp.onreadystatechange = callbackFunction(xmlhttp);
    xmlhttp.send(ItemJSON);
    alert(xmlhttp.responseText);
    return (xmlhttp.responseText)


    Thanks.
  • Tried the above snippet and it does not work. instead it says network error. Also tried to pass the credentials inside Open() without any headers and it throwed Access Error as shown below. Any advice please?

    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https..........", false,"usnmae","pwd");
    xhr.withCredentials = true;

    xht.send();

     

    Error:

    InvalidAccessError: InvalidAccessError

Reply Children
No Data