简体   繁体   English

如何通过JIRA REST API通过jQuery获取和发布数据?

[英]Howto get and post data via jQuery from JIRA REST API?

We are using JIRA 6.4.5 in our company but I am struggeling fetching data from its API REST interface. 我们正在公司中使用JIRA 6.4.5,但我正在努力从其API REST接口获取数据。 I have been trying now for the last couple of days, getting stuck on a cross-domain problem or that I don't know the user credentials so I cannot do any server-side either. 最近几天,我一直在尝试,遇到了跨域问题,或者我不知道用户凭据,因此也无法在任何服务器端进行操作。

Ideally I am having a jQuery page where the user will use his own credentials/session for querying the JIRA data. 理想情况下,我有一个jQuery页面,用户将在其中使用自己的凭据/会话来查询JIRA数据。 The JIRA REST API is located at srv1.mydomain.xyz and I am using srv2.mydomain.xyz as my webserver with my code. JIRA REST API位于srv1.mydomain.xyz ,我将srv2.mydomain.xyz用作我的代码的Web服务器。

I have read the JIRA REST API Reference . 我已经阅读了JIRA REST API参考

I have tried various Javascript/jQuery stuff - in the below example I am trying to submit 1h 30minutes to a specific issue: 我尝试了各种Javascript / jQuery东西-在以下示例中,我试图提交1h 30分钟以解决特定问题:

$.ajax({
    url: "https://srv1.mydomain.xyz/rest/api/latest/issue/proj-3/worklog",
    dataType: "json",
    method: "post",
    data: { time: "1h 30m",
            comment: "Test" }
}).done(function(data) {
    alert("Success");
}).fail(function(jqXHR, textStatus) {
    alert("Failed");
});

I get this error: 我收到此错误:

XMLHttpRequest cannot load https://srv1.mydomain.xyz/rest/api/latest/issue/proj-3/worklog?time=1h+30m&comment=Test . XMLHttpRequest无法加载https://srv1.mydomain.xyz/rest/api/latest/issue/proj-3/worklog?time=1h+30m&comment=Test No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' https://srv2.mydomain.xyz ' is therefore not allowed access. 因此,不允许访问源“ https://srv2.mydomain.xyz ”。 The response had HTTP status code 401. 响应的HTTP状态码为401。

I then looked more in to this and saw that Atlassian has something called Atlassian Connect so I tried with this: 然后,我进一步查看了此内容,发现Atlassian具有称为Atlassian Connect的功能,因此我尝试了以下操作:

AJS.$.ajax({
    url: "https://srv1.mydomain.xyz/rest/api/latest/issue/proj-3/worklog",
    type: "post",
    dataType: "json",
    contentType: "application/json",
    xhrFields: { withCredentials: true },
    async: false,
    method: "post",
    data: { time: "1h 30m",
            comment: "Test" }
}).done(function(data) {
    alert("Success");
}).fail(function() {
    alert("Failed");
});

But I get a similar error: 但是我得到一个类似的错误:

XMLHttpRequest cannot load https://srv1.mydomain.xyz/rest/api/latest/issue/proj-3/worklog . XMLHttpRequest无法加载https://srv1.mydomain.xyz/rest/api/latest/issue/proj-3/worklog Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“ Access-Control-Allow-Origin”标头。 Origin ' https://srv2.mydomain.xyz ' is therefore not allowed access. 因此,不允许访问源“ https://srv2.mydomain.xyz ”。

I have also looked in to if I could do this server-side from my PHP enabled server in the same domain as the JIRA server but as I don't get the base64 encoded credentials when doing a phpinfo() then I don't think I can use this approach either (and I don't want to prompt the user for credentials). 我还研究了是否可以在与JIRA服务器相同的域中从启用PHP的服务器执行此服务器端操作,但是由于在执行phpinfo()时未获得base64编码的凭据,因此我认为我也可以使用这种方法(并且我不想提示用户输入凭据)。

I am painfully aware that my problem is related to cross-domain protection but I cannot find any examples on how to fix it? 我很痛苦地意识到我的问题与跨域保护有关,但是找不到如何解决它的示例? It would be great if the JIRA server could set a Access-Control-Allow-Origin for certain hosts but I assume this is not a configuration option (I am not in control of the JIRA server). 如果JIRA服务器可以为某些主机设置Access-Control-Allow-Origin ,那就太好了,但是我认为这不是配置选项(我不受JIRA服务器的控制)。

This is definitely a cross-domain case. 这绝对是跨域的情况。 And believe me, it exists for your own protection ; 相信我,它的存在是为了保护您自己; )

The method I use is to send the jQuery request to a server-based processing page, which then authenticates and interracts with the Jira server. 我使用的方法是将jQuery请求发送到基于服务器的处理页面,然后对该页面进行身份验证并与Jira服务器进行交互。 In your case, since srv1 and srv2 are under the same domain, srv2 (webserver) can talk to srv1 (Jira) using internal IPs ( https://10.50.25.87:8080/rest/api/latest/issue/proj-3/worklog , for example) so the cross-domain issue doesn't apply. 在您的情况下,由于srv1和srv2在同一域中,因此srv2(Web服务器)可以使用内部IP与srv1(Jira)进行通信( https://10.50.25.87:8080/rest/api/latest/issue/proj-3 / worklog ),因此跨域问题不适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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