无法从普通JavaScript访问Jenkins API

无法从普通JavaScript访问Jenkins API,javascript,html,jenkins,jenkins-api,Javascript,Html,Jenkins,Jenkins Api,我对API和JavaScript相当陌生,所以我不确定我在这里做错了什么,请查找下面我面临的问题的详细信息 目标-在网页上获取詹金斯的详细信息 作为我目标的一部分,我正在尝试制作一个网页,从中我可以触发Jenkins作业并获得网页上显示的构建信息。作为目标的一部分,我想从我网页上的一个按钮触发詹金斯的工作 问题- 我创建了以下脚本来执行该操作- <script> function RunJenkinsJob() { var xhr = new

我对API和JavaScript相当陌生,所以我不确定我在这里做错了什么,请查找下面我面临的问题的详细信息

目标-在网页上获取詹金斯的详细信息

作为我目标的一部分,我正在尝试制作一个网页,从中我可以触发Jenkins作业并获得网页上显示的构建信息。作为目标的一部分,我想从我网页上的一个按钮触发詹金斯的工作

问题- 我创建了以下脚本来执行该操作-

<script>
    function RunJenkinsJob() {
        
        var xhr = new XMLHttpRequest();
        xhr.open("POST", http://admin:114a2eae5e09d93b6ee831f248892ac581@localhost:8080/job/New_Test/build?token=Datacreation, true);
        xhr.setRequestHeader('Content-Type', 'application/json');
    }));
        
    }
    
    </script>
    <head>
        <style>body{background-color:#d0e4fe;}h1{color:orange;text-align:center;}p{font-family:"Times New Roman";font-size:20px;}</style>
    </head>
    <body>
        <h1>Check Jenkins Job Build</h1>
        <button type="button" onclick="RunJenkinsJob();"> Run Jenkins Job! </button>
    </body>
    </html>

函数RunJenkinsJob(){
var xhr=new XMLHttpRequest();
xhr.open(“POST”,http://admin:114a2eae5e09d93b6ee831f248892ac581@localhost:8080/job/New_Test/build?token=Datacreation,true);
setRequestHeader('Content-Type','application/json');
}));
}
正文{背景色:#d0e4fe;}h1{颜色:橙色;文本对齐:居中;}p{字体系列:“Times New Roman”;字体大小:20px;}
检查Jenkins作业构建
去干詹金斯的工作!
但是,当我尝试单击此按钮时,什么都没有发生,直接从浏览器访问此URL时,会要求我提供用户名和密码,然后在刷新新版本后会自动触发

问题

如何在这个JavaScript方法中为Jenkins提供身份验证,以便在单击按钮时可以触发新作业,并且如果我能够获得一些关于如何提取构建信息的指针,这也将非常有用

系统详细信息 操作系统-Windows 詹金斯版本-2.235.1

--更新1-

请在下面查找更新的详细信息-

<!DOCTYPE html>
<html>
    <script>
    
    function RunJenkinsJob() {
        
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "http://admin:114a2eae5e09d93b6ee831f248892ac581@localhost:8080/job/New_Test/build?token=Datacreation", true);
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.send();
        document.write(xhr.status);
        
    }
    
    </script>
    <head>
        <style>body{background-color:#d0e4fe;}h1{color:orange;text-align:center;}p{font-family:"Times New Roman";font-size:20px;}</style>
    </head>
    <body>
        <h1>Check Jenkins Job Build</h1>
        <button type="button" onclick="RunJenkinsJob();"> Run Jenkins Job! </button>
    </body>
    </html>

函数RunJenkinsJob(){
var xhr=new XMLHttpRequest();
xhr.open(“POST”http://admin:114a2eae5e09d93b6ee831f248892ac581@localhost:8080/job/New_Test/build?token=Datacreation“,true);
setRequestHeader('Content-Type','application/json');
xhr.send();
文件写入(xhr状态);
}
正文{背景色:#d0e4fe;}h1{颜色:橙色;文本对齐:居中;}p{字体系列:“Times New Roman”;字体大小:20px;}
检查Jenkins作业构建
去干詹金斯的工作!

以下代码对我有效

将此应用于您的run jenkins作业功能。这将有助于调试,如果它循环通过所有4个状态更改和接收到的状态

var xhr = new XMLHttprequest();
var authorizationbasic = window.btoa("username:token");
xhr.onreadystatechange = function() {
    if(this.readyState == 4)
    {
         console.log(this.status);
    }
};
xhr.open("POST", "http://admin:114a2eae5e09d93b6ee831f248892ac581@localhost:8080/job/New_Test/build?token=Datacreation", true);
xhr.setRequestHeader('Authorization','Basic ' + authorizationbasic)
xhr.send();

如果您的状态仍然为0,则可以检查jenkins安装中的
CORS过滤器设置。

您需要将API URL加引号,以便JavaScript将其解释为字符串。我也觉得像〈代码〉)
不属于,应该删除。您好@ShaneBishop,谢谢您的宝贵意见,我已经做了这些更改并重试,但仍然没有回应。您应该编辑您的问题,以包括到目前为止的修复。
管理员:114a2eae5e09d93b6ee831f248892ac581@localhost:8080
我不确定詹金斯是否仍然支持这一点。这将导致重定向。您需要在身份验证头中将用户id admin和api令牌作为base64编码字符串传递。还缺少
xhr.send()
。您可以通过在控制台日志中打印来检查状态吗`console.log(xhr.status)“”在发送行之后?Hi@SiddharthKaul,我尝试添加xhr.send(),但结果并不像预期的那样。运行xhr.status()网页时返回0。您好,这个答案非常有用,在检查我的响应后,我必须将CORS筛选器安装到我的jenkins上,并且我能够成功触发我的请求。我注意到两件事:1)URL可以没有用户名和令牌;2)CORS插件非常重要