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

时间:2015-11-07 19:10:11

标签: javascript jquery jira-rest-api

我试图通过使用jQuery.ajax函数在本地运行的HTML中调用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实现此功能的任何提示吗?

2 个答案:

答案 0 :(得分:0)

谢谢@Andreas!

<强>解决方案

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

注意,这只是我本机上本地CORS问题的临时解决方案,仅用于测试目的。

答案 1 :(得分:0)

对我们有帮助的是在将请求转发给JIRA之前删除Apache代理中的User-Agent标头

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //All your other initialisations

    //Get any potential intent that was passed to the activity
    Intent intent = getIntent();
    if (intent != null) {
      //Is there a string extra in that intent?
      String extra = intent.getStringExtra("methodName");
      //Is that string not empty and equals what you're looking for?
      if (!TextUtils.isEmpty(extra) && extra.equals("myMethod")) {
        myView.post(new Runnable() { 
          @Override 
          public void run() { 
              myView.smoothScrollTo(0, hdr3a.getTop()); 
          } 
        }); 
      }
  }
} 

这也需要在Apache中启用mod_headers。