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

Katie Jordan October 13, 2012

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

1 vote
sonika jain August 21, 2015

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');
        }
0 votes
Swaraj gochhayat February 11, 2013

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.

0 votes
Deniss Alimovs February 11, 2013

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'
});
0 votes
Swaraj gochhayat January 28, 2013
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.
0 votes
Daria Trainor October 14, 2012

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/

Suggest an answer

Log in or Sign up to answer