简体   繁体   中英

Jenkins POST Request with username/API Token javascript

I'm using a chrome extension to display and visualize data from a jenkins server. I tried to start a certain build on that server using the jQuery function $.ajax(), but since the server is secured, I need to pass a username and password.

After googling for a while I found a lot of people suggesting this format:

http://username:APIToken@jenkinsurl

I've tried both this, and a combination of username and the actual password, but nothing seemed to work.

I have to note that I'm using the Jenkins Test Drive , and http://localhost:8080/ for my purposes, since I don't actually own a jenkins server. Maybe that has something to do with the above not working.

Turns out that jenkins requires preemptive authentication, which means that all I ended up doing, was adding the 'beforeSend' option to the ajax call.

$.ajax({
        type: "POST",
        beforeSend: function (xhr){
            xhr.setRequestHeader('Authorization', "Basic " + btoa("USERNAME" + ":" + "APITOKEN"));
        },
        url: "Jenkins-Job-Url/build?token=TOKEN"
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM