How can I make a call to REST API using JQuery, Cross domain to get the list of issues in Jira?

I'm developing a SharePoint2013 application in which I can only use Client Scripts (such as JQuery). I have to show list of JIRA issues assigned to the current logged on user. The app is deployed to a different hos than SharePoint so, I need to make a cross domain script call (JSONP). I have tried a lot of different things including the one below and non of them has worked. Please let me know what should I do to make this call?

BTW, I have tried the parameters used for $.ajax({}) in curl and it's returning expected response. it's just the usage of jsonp (perhaps) that is not right in my code (below):

Thanks in advance

$(
'#ajax').click(function () {
$.ajax({
type:
"GET",
"Content-Type": "application/json",
dataType:
"jsonp",
url:
"https://brcmdemo.atlassian.net/rest/api/2/issue/DEMO-1",
success:
function (data) {
alert(
"hello there!");
alert(data)
},
error:
function () {
alert(
'failed');
},
beforeSend:
function (xhr) {
xhr.setRequestHeader(
"Authorization", "Basic a2F0aWVqOnBhc3NAd29yZDE=");
}
});
});
});

5 answers

Hello Everyone !!!,

I am facing issue in JIRA rest call using jquery in my web application . Below is the sample code that i have used.  Below sample code giving following error 

Refused to execute script from 'https://test.atlassian.net/rest/api/2/serverInfo?jsonpcallback=jQuery17105837092609144747_1440226296872&_=1440226299980' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.

 

$.ajax({
            url: 'https://test.atlassian.net/rest/api/2/serverInfo',
            dataType: 'jsonp',
            jsonp: 'jsonpcallback',
            beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", "Basic username:password in encode form);
             },
        });
        function jsonpcallback(data) {
            alert('inside jsonpcallback');
        }

I think you have stumbled on the known limitation of JSONP - it does not handle 401 errors.

You can read about it here: http://forum.jquery.com/topic/jquery-ajax-with-datatype-jsonp-will-not-use-error-callback-if-request-fails

There are several ways of resolving it. One of them is using timeout:

http://www.haykranen.nl/2011/02/25/jquery-1-5-and-jsonp-requests/

You can successfully login by making a GET request to the URL 'https://XXXXXXX.atlassian.net/rest/auth/1/session?os_username=yourusername&os_password=yourpass'
Since this is cross domain request you need to handle it using JSONP but the server returns only JSON data which leads to another error:
SyntaxError: invalid label in Mozilla.
Uncaught SyntaxError: Unexpected token : in Chrome
$.ajax({
type: 'GET',
url: 'https://XXXXXXX.atlassian.net/rest/auth/1/sessionos_username=yourusername&os_password=yourpass',
contentType: "application/jsonp",
crossDomain: true,
dataType: 'jsonp',
jsonpCallback: 'jsonpCallback',
success: function (response) {
alert('success--' + response);
},
error: function (response) {
alert(response);
}
});

Please let me know if you get any solution to this.
Thanks in Advance.

Make sure your dataType is 'jsonp' and jsonp param is 'jsonp-callback'. This works with version two api (https://[yourSubDomain].atlassian.net/rest/api/2/)

$.ajax({
url: 'https://[yourSubDomain].atlassian.net/rest/auth/2/[yourRequest],
dataType: 'jsonp',
jsonp: 'jsonp-callback'
});

I tried this way..

var data= {

"username": "myusername",

"password": "mypassword"

};

$.ajax({

url: https://XXXXXXX.atlassian.net/rest/auth/2/session,

type: POST,

dataType: 'jsonp',

jsonp: 'jsonp-callback',

data: data,

crossDomain: true,

success: function (data) { alert('success!' + data); },

error: function (data) { alert("error!!" + data); }

});


Now i get 404 not found error. in version 2
But Version 1 was returning 415 unsupported media type.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

27,640 views 2 7
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you