Rajesh Kazhankodath
Rajesh Kazhankodath

Reputation: 409

CORS error with jenkins job invoked from javascript buildWithParameters

I have the following javascript code that is makes a POST request a Jenkins Job.

<script>

function callJenkinsJob() {
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "https://<user>:<token>@<JENKINBS_SERVER>/job/<JOB_NAME>/buildWithParameters", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
    //xhr.setRequestHeader("Sec-Fetch-Mode", "no-cors");
    xhr.setRequestHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
    xhr.setRequestHeader("Access-Control-Allow-Headers", "Authorization")
    xhr.setRequestHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, observe");
    xhr.setRequestHeader("Access-Control-Max-Age", "3600");
    xhr.setRequestHeader("Access-Control-Allow-Credentials", "true");
    var param1 = get_base_url()
    xhr.send("MY_URL=" + param1);
}
function get_base_url() {
    var base_url = document.getElementById('base_url');
    console.log(base_url.innerText)
    return base_url.innerText
}
    </script>
    <td align=right>
        <button onclick="callJenkinsJob()">Click</button>
    </td>

I'm getting the following CORS error when the jenkins job is invoked in the console log of the brower. ( Edge Version 108.0.1462.54 (Official build) (64-bit) )

Access to XMLHttpRequest at 'https://<user>:<token>@<JENKINBS_SERVER>/job/<JOB_NAME>/buildWithParameters' from origin 'http://<server>' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Jenkins version is 2.332.1 and "CORS support for Jenkins Version1.1" are installed.

Any idea what may be wrong with the request? ---Updated Info:--- enter image description here

CORS Filter configurations: ( Available under https://<JENKINS_SERVER>/configure enter image description here

Access-Control-Allow-Headers : Access-Control-Allow-Origin,access-control-allow-credentials,access-control-allow-origin,Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, observe, access-control-max-age, access-control-allow-methods

Upvotes: 0

Views: 662

Answers (1)

Sahil Sharma
Sahil Sharma

Reputation: 11

Cors filter plugin is broken. It should be 'access-control-allow-origin' instead of 'access-control-allow-origins' there is an extra s at the end https://community.jenkins.io/t/cors-errors-when-executing-remote-jobs-through-api/2054

Upvotes: 1

Related Questions