Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use ajax to trigger jenkins job build?

Tags:

ajax

jenkins

I'm writing a web page to let others can trigger the some jobs' build with parameters in jenkins. So I use ajax to send POST request:

var urlString = "http://localhost:8080/job/myjob/buildWithParameters";

$.post(
    urlString,
    {myParam:"there is some data"},
    function(data)
    {
        alert(data);
    },
    "json"
);

But I got Http 403 response:

XMLHttpRequest cannot load http://localhost:8080/job/myjob/buildWithParameters. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 403.

I know the cross site problem , but I cannot search any helpful information from Google, can ajax do this job?

UPDATE: I found a similar question

So I update my code to :

$.ajax({                              
    type: "POST",
    url: urlString,
    dataType: 'jsonp',
    data: {},
    beforeSend: function(xhr){
        xhr.setRequestHeader("Authorization", "Basic " + btoa("admin:123456"));
    },
    success: function(data) {
    },             
    complete: function(xhr, statusText){
    }                                                                 
});

I can confirm the username and password is correct , but I got 405 Method Not Allowed. Is there anything wrong?

like image 300
zzy Avatar asked Dec 06 '25 07:12

zzy


1 Answers

Put your web page in the userContent folder under $JENKINS_HOME directory. Then open $JENKINS_URL/userContent/yourwebpage.html in your browser. Now the javascript in the page is loaded from the same origin where ajax calls will go, so it should be allowed without CORS tricks.

like image 91
sti Avatar answered Dec 08 '25 21:12

sti