简体   繁体   English

从本地HTML文件中的Javascript调用JIRA REST Api

[英]Calling JIRA REST Api from Javascript in local HTML file

I´m trying to call JIRA REST Api from javascript in HTML running locally by using jQuery.ajax function like below: 我正在尝试使用如下所示的jQuery.ajax函数从本地运行的HTML中的javascript调用JIRA REST Api:

$.ajax({
    type: "GET",
    url: "http://jira.myhost.com/rest/api/2/issue/AB-123",
    dataType: "json",
    contentType: "application/json",
    async: false,
    beforeSend: function (xhr) {
        var base64 = "YWRtaW46YWRtaW4";
        xhr.withCredentials = true;
        xhr.setRequestHeader("Authorization", "Basic " + base64);
    }
})
.fail(function (error) {
    console.log(error.statusText)
});

When opening HTML file in Chrome I get following error in console: 在Chrome中打开HTML文件时,在控制台中出现以下错误:

NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://jira.myhost.com/rest/api/2/issue/AB-123'.

However, I can successfully access the same JIRA REST Api by using CURL locally: 但是,我可以通过本地使用CURL成功访问相同的JIRA REST Api:

curl -D- -X GET -H "Authorization: Basic YWRtaW46YWRtaW4" -H "Content-Type: application/json" "http://jira.myhost.com/rest/api/2/issue/AB-123"

Any tip on how to get this working from JavaScript? 关于如何从JavaScript进行工作的任何技巧?

Thanks @Andreas! 谢谢@Andreas!

Solution

open /Applications/Google\ Chrome.app/ --args --disable-web-security

Note , this is just a temporary solution on CORS issues locally on my machine and is only used for testing purpose. 请注意 ,这只是在我的计算机上本地解决CORS问题的临时解决方案,仅用于测试目的。

What helped for us was removing the User-Agent header in our Apache proxy before forwarding the request to JIRA 对我们有用的是在将请求转发到JIRA之前删除了Apache代理中的User-Agent标头

<Location "/rest">
    RequestHeader unset User-Agent
</Location>

This also requires mod_headers enabled in Apache. 这也需要在Apache中启用mod_headers。

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

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