2

I have a rest call which is basically a GET request.Iam trying to get that using below code

var url ='https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets/6662c0f2.ee6a64fe.1mdlmv34b.
anfhlbg.dmjrjk.5ct1qigk0ndbkd22sqp8i';
$http({
   method: 'GET',
    url: url ,
    headers: {
          'Authorization': 'Basic aXNhZG1pbjppbmYwc2VydmVy'}
})
.success(function (data) {
//  var res = JSON.parse(data);
  alert(data);
})
.error(function (error, status){
   // $scope.data.error = { message: error, status: status};
    console.log(error,status); 
});

But it is giving below CORS issue.

enter image description here

So I tried to use jsonp approach using the following code

`$.get({
        url: url,
        headers: {
          'Authorization': 'Basic aXNhZG1pbjppbmYwc2VydmVy'},
         dataType: "JSONP",
         jsonpCallback: 'callback',
         success: function func(json){
              alert(json.name);
    }
});`

This way Iam getting below error

enter image description here

But in network tab,I see 200 code and also response from that call.

enter image description here

But unable to capture.Is there a way that I can capture that response in my code..Can someone help me..Iam facing this issue from some time and no could resolve this.Can someone help me.Thanks!

****Edit **Adding the Json response which is valid json

{
"created_by": "Administrator IIS",
"modified_on": "2017-05-29T13:09:42Z",
"_context": [],
"created_on": "2017-04-17T09:50:03Z",
"terms": {
    "items": [{
        "_type": "term",
        "_id": "6662c0f2.e1b1ec6c.1mdlmv35e.1618tt1.8mb4gb.nahef1pustcmu5k10omdh",
        "_url": "https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets/6662c0f2.e1b1ec6c.1mdlmv35e.1618tt1.8mb4gb.nahef1pustcmu5k10omdh",
        "_name": "Export_01"
    }, {
        "_type": "term",
        "_id": "6662c0f2.e1b1ec6c.1mdln1e8h.0ibdeqc.s1nm3u.vbt90ta1civ5asp5qetu5",
        "_url": "https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets/6662c0f2.e1b1ec6c.1mdln1e8h.0ibdeqc.s1nm3u.vbt90ta1civ5asp5qetu5",
        "_name": "Export_02"
    }, {
        "_type": "term",
        "_id": "6662c0f2.e1b1ec6c.1mdln7r4n.tn8gjjo.rd5uri.25i83fvkbo5l4bhscd3dr",
        "_url": "https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets/6662c0f2.e1b1ec6c.1mdln7r4n.tn8gjjo.rd5uri.25i83fvkbo5l4bhscd3dr",
        "_name": "Mule Asset 01"
    }, {
        "_type": "term",
        "_id": "6662c0f2.e1b1ec6c.1mdln7rs7.3tg971k.ghjkja.lnc3bpu3h6pst1tik15ij",
        "_url": "https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets/6662c0f2.e1b1ec6c.1mdln7rs7.3tg971k.ghjkja.lnc3bpu3h6pst1tik15ij",
        "_name": "Mule Asset 02"
    }],
    "paging": {
        "numTotal": 4,
        "pageSize": 10,
        "end": 3,
        "begin": 0
    }
},
"_name": "DGC2IGC",
"_type": "category",
"_id": "6662c0f2.ee6a64fe.1mdlmv34b.anfhlbg.dmjrjk.5ct1qigk0ndbkd22sqp8i",
"short_description": "This is for Mulesoft Data Movement",
"modified_by": "Administrator IIS",
"_url": "https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets/6662c0f2.ee6a64fe.1mdlmv34b.anfhlbg.dmjrjk.5ct1qigk0ndbkd22sqp8i",
"name": "DGC2IGC",
"development_log": {
    "items": [{
        "person": "Administrator IIS",
        "new_state": "Synched",
        "workflow_task": "NEW",
        "user_id": "isadmin",
        "comment": "Public comment",
        "date": "2017-04-17T09:57:19Z",
        "activity": "PUBLISHED"
    }, {
        "person": "Administrator IIS",
        "new_state": "Approved",
        "workflow_task": "NEW",
        "user_id": "isadmin",
        "comment": "Approved!",
        "date": "2017-04-17T09:56:48Z",
        "activity": "Approved"
    }, {
        "person": "Administrator IIS",
        "new_state": "Pending Approval",
        "workflow_task": "NEW",
        "user_id": "isadmin",
        "comment": "Approve!",
        "date": "2017-04-17T09:56:20Z",
        "activity": "SentForApproval"
    }, {
        "person": "Administrator IIS",
        "new_state": "Draft",
        "workflow_task": "NEW",
        "user_id": "isadmin",
        "comment": "Comment01",
        "date": "2017-04-17T09:50:03Z",
        "activity": "NEW"
    }],
    "paging": {
        "numTotal": 4,
        "beginIndex": 0,
        "endIndex": 3,
        "pageSize": 4
    }
}

}

user7350714
  • 365
  • 1
  • 6
  • 20
  • Is that JSON from that response you see in the network tab really well formed? Is it valid JSON? Can you copy there and paste it here? – acdcjunior Jun 15 '17 at 16:48
  • @acdcjunior yes,it is a valid json..Posting in my question.How can I cappture that>Is there any way? – user7350714 Jun 15 '17 at 16:50
  • The response in the question is just JSON data, not in the expected JSONP format. That is, your response is like this: `{ "bar": "baz" }` but JSONP is like this: `foo( { "bar": "baz" } )`. So you’re telling your jQuery code to expect JSONP but what you’re giving is not JSONP. It seems likely the `https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets` API endpoint you’re making the request to isn’t actually capable of sending JSONP responses. – sideshowbarker Jun 15 '17 at 17:00
  • @sideshowbarker ohh..Then is there any way that I can capture data of json type? – user7350714 Jun 15 '17 at 17:02
  • No, as long as the `https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets/` server isn’t following the CORS protocol there no way you can make a request directly to it from your frontend JavaScript code and capture the response. Your browser will continue to block your frontend code from accessing the response. Your only solution is either to instead make the request from your backend code or else set up a CORS proxy of some kind through which your frontend code could make the request. – sideshowbarker Jun 15 '17 at 17:08
  • See https://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource/42744707#42744707 for how-to details on setting up your own CORS proxy – sideshowbarker Jun 15 '17 at 17:08
  • @sideshowbarker The issue here is we cant make any changes to the server side code.This is not in my control.So I have to figure it out in my client side itself.so I used jsonp way – user7350714 Jun 15 '17 at 17:12
  • Like I said, even if you can’t make changes to the server-side code of `https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets`, you can set up your own proxy in between. Again, see https://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource/42744707#42744707 – sideshowbarker Jun 15 '17 at 17:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146786/discussion-between-user7350714-and-sideshowbarker). – user7350714 Jun 15 '17 at 17:43
  • @sideshowbarker CAn you give me example how can we do in my case.I tried but no luck – user7350714 Jun 15 '17 at 18:13

0 Answers0