3

I want to run a Jenkis job that requires login from a web page (outside of jenkins) using ajax currenly i have to have another browser tab with jenkins open (and authenticated) in order for the job to run from my web page

iv'e tried different approaches to sending the authentication info with ajax this is what i currently have:

$.ajax({                              
    type: "POST",
    url: "http://myjenkins/job/job_name/buildWithParameters",
    dataType: 'jsonp',
    data: $("#myForm").serialize(),
    beforeSend: function(xhr){
    xhr.setRequestHeader("Authorization", "username:password");
    },
    success: function(data) {
    },             
    complete: function(xhr, statusText){
    }                                                                 
});

(there's some more HTML code that receives parameters from a form) if i have an open tab with jenkins authenticated this runs fine, but if i don't i get a "430 forbidden" responce from jenkins.

the "xhr.setRequestHeader("Authorization", "username:password");" is just my latest attempt...

any input is welcome!

Igoros
  • 63
  • 1
  • 5
  • xhr.setRequestHeader('Authorization ', make_base_auth(user, password)); -- from http://stackoverflow.com/questions/10226333/ajax-authentication-with-jquery -- Don't know if it helps since I hae never worked with ajax. ;) – Peter Schuetze Oct 03 '13 at 18:22
  • unfortunately i have already tried that, doesn't seem to work... thanks for the input. – Igoros Oct 06 '13 at 07:39

2 Answers2

0
beforeSend: function (xhr) {
    xhr.withCredentials = true;
    data: $("[name='parameters']").serialize()
    xhr.setRequestHeader("Authorization", "Basic " + btoa("test:d8db6ae61f03219c52042638488e9744"));
},

works for me

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
qingfu
  • 9
  • 1
0

This worked for me, thanks a lot, just to expand, it worked because As per RFC 1945, the Authorization header value should contain the username:password as encoded (base64) string, which would result in something like the following header:

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

example:

beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("username:token"));
}