从本地 HTML 文件中的 Javascript 调用 JIRA REST Api
Calling JIRA REST Api from Javascript in local HTML file
我正在尝试通过使用 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)
});
在 Chrome 中打开 HTML 文件时,控制台出现以下错误:
NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://jira.myhost.com/rest/api/2/issue/AB-123'.
但是,我可以在本地使用 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"
关于如何从 JavaScript 获得此功能的任何提示?
谢谢@Andreas!
解决方案
open /Applications/Google\ Chrome.app/ --args --disable-web-security
注意,这只是我机器本地CORS问题的临时解决方案,仅用于测试目的。
对我们有帮助的是在将请求转发到 JIRA 之前删除 Apache 代理中的 User-Agent header
<Location "/rest">
RequestHeader unset User-Agent
</Location>
这还需要在 Apache 中启用 mod_headers。
我正在尝试通过使用 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)
});
在 Chrome 中打开 HTML 文件时,控制台出现以下错误:
NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://jira.myhost.com/rest/api/2/issue/AB-123'.
但是,我可以在本地使用 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"
关于如何从 JavaScript 获得此功能的任何提示?
谢谢@Andreas!
解决方案
open /Applications/Google\ Chrome.app/ --args --disable-web-security
注意,这只是我机器本地CORS问题的临时解决方案,仅用于测试目的。
对我们有帮助的是在将请求转发到 JIRA 之前删除 Apache 代理中的 User-Agent header
<Location "/rest">
RequestHeader unset User-Agent
</Location>
这还需要在 Apache 中启用 mod_headers。