3

i'm trying to make a simple ajax request to the following URL. https://insightsoftwaresolutions.atlassian.net/rest/api/2/issue/createmeta?projectKeys=TES&issuetypeNames=Bug&expand=projects.issuetypes.fields

It receives the JSON response when i just put the URL on browser navigation bar and press enter but it's not working when i try to make a jquery ajax call. It's not having any console errors.

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>

    <script>
        $(document).ready(function () {

            $.ajax({
                cache: false,
                type: 'GET',
                crossDomain: true,
                url: 'https://insightsoftwaresolutions.atlassian.net/rest/api/2/issue/createmeta?projectKeys=TES&issuetypeNames=Bug&expand=projects.issuetypes.fields',
                contentType: 'application/json; charset=utf-8',
                dataType: 'jsonp',

                success: function (data) {
                    alert("success");
                },
                    error: function (jqXHR, textStatus) {
                        //displayCallResults(jqXHR);
                        alert("error");
                    }
            });

        });
    </script>  

UPDATE:

I changed the datatype:'jsonp' to datatype:'json'. Then i get the following error.

Origin http://localhost:3029 is not allowed by Access-Control-Allow-Origin.
chamara
  • 12,649
  • 32
  • 134
  • 210
  • What is in the net tab? – KnowHowSolutions Oct 02 '13 at 03:19
  • 3
    It doesn't look like the said api is supporting jsonp – Arun P Johny Oct 02 '13 at 03:19
  • So, what is the error message and error code instead of just alert('error')? Also, have you used Fiddler to inspect the request and response? Have you tried POST instead of GET? – Valamas Oct 02 '13 at 03:19
  • please add callback=? because it is necessary when want to get data through jsonp http://www.jquery4u.com/json/jsonp-examples/ – Mudaser Ali Oct 02 '13 at 03:26
  • it's the client JSON parser having trouble parsing the response the result of the query is this {"expand":"projects","projects":[]} – jerjer Oct 02 '13 at 03:29
  • Try to figure out what the error message shown on network tab of firebug. could help us more for troubleshooting. – ram Oct 02 '13 at 03:31
  • @ArunPJohny please refer to my update – chamara Oct 02 '13 at 03:31
  • @MudaserAli i tried but did not work – chamara Oct 02 '13 at 03:33
  • One more thing whatever value is passed through callback you have to encapsulate your json object in that. like for example in php we use echo $_GET['callback'].'(' . json_encode($data).')'; Ref : http://stackoverflow.com/questions/6809053/simple-jquery-php-and-jsonp-example – Mudaser Ali Oct 02 '13 at 03:33
  • I guess you can find solution from http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – Andrew Liu Oct 02 '13 at 03:35
  • callbak is not mandatory using jQuery because jQuery itself if omitted undertakes d puts it. The case is that the URL JSON is not returning anything because the array response 'projects' is empty. Theoretically the request is ok. Requests cross-platform work only with type JSONP – Walter Gandarella Oct 02 '13 at 03:41
  • 1
    @WalterBarreiroNeto since the response is received, even though the array is empty i should ended up with "success" alert right? so why is it ended up receiving "error" alert? – chamara Oct 02 '13 at 03:50
  • is right, try removing the lines `cache: false,` and `contentType: 'application/json; charset=utf-8',` and let your code less error prone for we analyze. test this – Walter Gandarella Oct 02 '13 at 03:54
  • JSON-P is no longer supported. See [JIRA 6 notes](https://developer.atlassian.com/display/JIRADEV/Preparing+for+JIRA+6.0#PreparingforJIRA6.0-JSON-Pnolongersupported) and [Razaq Omar (Atlassian)'s response to a similar inquiry](https://answers.atlassian.com/questions/138618/jira-rest-api-response-suddenly-stopped-sending-jsonp-getting-invalid-label-syntax-error). JSONP client-side makes your applications insecure and Atlassian decided to protect you from taking the risk (helpful or not). – zyglobe Oct 02 '13 at 04:09
  • change to dataType: 'json' – Jianhong Oct 02 '13 at 05:53

1 Answers1

2

Your Server does not support JSONP. Either change that

OR

Add headers at nginx to support CORS on the server side. OR you can add the CORS header on server side.

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");

Once you do that you can access your code using simple

$.getJSON(url).done(function(response) {
    console.log(response); //here's your response
});
Community
  • 1
  • 1
Pradeep Mahdevu
  • 7,613
  • 2
  • 31
  • 29