# AJAX Preflight Authentication for Kibana dashboard in an iframe

**URL:** <https://discuss.elastic.co/t/ajax-preflight-authentication-for-kibana-dashboard-in-an-iframe/204267>\
**Category:** Kibana\
**Created:** [October 18, 2019, 6:51pm UTC](https://discuss.elastic.co/t/ajax-preflight-authentication-for-kibana-dashboard-in-an-iframe/204267 "2019-10-18T18:51:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![druffner](https://avatars.discourse-cdn.com/v4/letter/d/a4c791/32.png) [@druffner](https://discuss.elastic.co/u/druffner)\
**Post date:** [October 18, 2019, 6:51pm UTC](https://discuss.elastic.co/t/ajax-preflight-authentication-for-kibana-dashboard-in-an-iframe/204267/1 "2019-10-18T18:51:55Z")

</div>

Hey everyone,

I am working on a webpage that will show my Kibana dashboard in an iframe, and I want to send a preflight AJAX request to login to the dashboard before setting the iframe's source. I have been following several posts on here that discuss this, but unfortunately I can't get my code to work. I am working with Kibana 6.8. Please excuse my ignorance, as I am somewhat new to web development.

On document.ready, I run an AJAX POST request that looks like this:

```
$.ajax({
    type: "POST",
    xhrFields: {
        withCredentials: true
    },
    data: "test",
    contentType: "application/x-www-form-urlencoded",
    crossDomain: true,
    beforeSend: function(xhr) {
        xhr.setRequestHeader('Authorization', btoa("username" + ":" + "password"));
    },
    headers: {
        'Access-Control-Allow-Credentials' : true,
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
        'Access-Control-Allow-Headers': 'X-Requested-With,content-type,Authorization',
    },
    url: "/api/security/v1/login",
    dataType: "json",
    success: ajaxSuccess,
    error: ajaxError
});

```

When this request runs, I get a 401 Unauthorized error. I have double checked to make sure that I'm not passing an incorrect username or password. If anyone could help me on this I would really appreciate it, as I am currently stuck.

---

<div class="post-metadata">

**Author:** ![markov00](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/markov00/32/33316_2.png) [@markov00](https://discuss.elastic.co/u/markov00)\
**Post date:** [October 28, 2019, 5:45pm UTC](https://discuss.elastic.co/t/ajax-preflight-authentication-for-kibana-dashboard-in-an-iframe/204267/2 "2019-10-28T17:45:03Z")

</div>

I'm not 100% sure if this is the only issue on that call, but the first think I can see is that you are missing the `Basic ` part on the authorization header string.

> [@druffner](#):
>
> xhr.setRequestHeader('Authorization', btoa("username" + ":" + "password"));

the Authorization header should be something similar to:

```auto
Authorization: Basic ZWxhc3RpYzo2RUVFZEVDTUNUTHRIalhudVJqSw==

```

so in your case I think you have to include:

```auto
xhr.setRequestHeader('Authorization', "Basic "+btoa("username" + ":" + "password"));

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 25, 2019, 5:45pm UTC](https://discuss.elastic.co/t/ajax-preflight-authentication-for-kibana-dashboard-in-an-iframe/204267/3 "2019-11-25T17:45:12Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
