Jira rest client in javascript giving error

萝らか妹 提交于 2019-12-11 10:34:29

问题


<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){

$.ajax({
            type: "GET",
            dataType: 'jsonp',
            url: "http://myJira.com/rest/api/2/issue/MA-6614/comment",
            username : "myusrName",
            password : "myPwd",
            success: function (data) {
                 console.log( "Sample of data:", JSON.stringify(data)); 

            },
            error: function (errormessage) {
              console.log( "errorMessage:", errormessage); 
            }
        });
});


</script>
</head>
<body> 
</body>
</html>

While running the above code I am getting SyntaxError: missing ; before statement error. I read somewhere Access-Control-Allow-Origin should be used to solve problem. But I don't find a good documentation anywhere about how to use with Jira.

"UPDATE"
The above error I am getting if I have already loggedin in jira. If I logout in jira and then run the above code then getting error "NetworkError: 401 Unauthorized - http://myjira.com/rest/api/2/issue/MA-6614/comment?callback=jQuery1102010440085066514837_1388216960965&_=1388216960966"

It seems the above code has two problem. 1. It is not able to authenticate in jira. 2. if we already authenticate in jira (that means browser has cookies)and make the request then syntax error is shown because of jsonp.


回答1:


So I got the answer. Support in jira is revoked for authenticating from javascript. Iitally jsonP was the option but now it is not possible. We have to do server side authentication of jira. Example here

https://developer.atlassian.com/display/JIRADEV/JIRA+REST+API+Example+-+Basic+Authentication




回答2:


You can do this with javascript.

function request(){
  var xhr = getXHR();

  xhr.addEventListener("error", transferFailed, false);
  xhr.addEventListener("abort", transferCanceled, false);

  xhr.open("GET", baseURL+"/rest/api/2/issue/KEY-1", false);
  xhr.setRequestHeader("Authorization", "Basic "+btoa("admin:admin"))
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.send();
}

Ensure that it is synchronous and jira server conf has:

Header always set Access-Control-Allow-Credentials "true" Header set Access-Control-Allow-Headers "Authorization, Origin, X-Requested-With, Content-Type, Accept"



来源:https://stackoverflow.com/questions/20812158/jira-rest-client-in-javascript-giving-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!