从javascript buildWithParameters调用jenkins作业时出现CORS错误

fkaflof6  于 2023-03-22  发布在  Jenkins
关注(0)|答案(1)|浏览(165)

我有下面的JavaScript代码,它使POST请求成为Jenkins作业。

<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>

当浏览器的控制台日志中调用jenkins作业时,我得到以下CORS错误。(Edge版本108.0.1462.54(官方构建)(64位))

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版本为2.332.1,并安装了“CORS support for Jenkins Version1.1”。
你知道这个请求可能有什么问题吗?---更新信息:---x1c 0d1x
CORS过滤器配置:(可在https://<JENKINS_SERVER>/configure

下获得
访问控制允许标头: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

uelo1irk

uelo1irk1#

Cors过滤器插件已损坏。它应该是'access-control-allow-origin'而不是'access-control-allow-origins',结尾处有一个额外的s https://community.jenkins.io/t/cors-errors-when-executing-remote-jobs-through-api/2054

相关问题