0

I'm making a cross domain ajax call. When I heat the link directly from my browser, I get the json strin as follows:

enter image description here

But when I'm making an ajax call to this same URL, I'm not getting the json in my response. My ajax request is as follows:

$.ajax({
  url: "http://172.16.201.14:801/api/apiclearing?trackNo=" + $scope.BillClearingModel.BillTrackingNo + "&&tokenNo=~Ice321",
  dataType: 'jsonp',
  success: function(response) {
    console.log(response);
    alert("Success");
  },
  error: function(response) {
    console.log(response);
    alert("Failed");
  }
});

What Im getting in console is as follows:

enter image description here

Full Object is as follows:

enter image description here

What I'm missing here? Thanks.

Badhon Jain
  • 938
  • 6
  • 20
  • 38
  • Should the tocken be a header? Beucase I cannot see it in the browser request. – Alex Suleap Aug 23 '16 at 06:13
  • you're loading **http:** in a **https:** page - browsers don't like that – Jaromanda X Aug 23 '16 at 06:14
  • I know, but ain't there anything to overcome the issue? @jaromandaX – Badhon Jain Aug 23 '16 at 06:18
  • should change http to https. How your server throw data? – c.k Aug 23 '16 at 06:19
  • @Badhon. No, there is no way to load HTTP resource into HTTPS page. In almost all browsers it is impossible. –  Aug 23 '16 at 06:19
  • yes, don't try to get a **http:** resource from a **https:** page – Jaromanda X Aug 23 '16 at 06:19
  • [Possible diplicate](http://stackoverflow.com/questions/18251128/why-am-i-suddenly-getting-a-blocked-loading-mixed-active-content-issue-in-fire) –  Aug 23 '16 at 06:20
  • you did missed the method: $.get(), $.post(), or $.load() – c.k Aug 23 '16 at 06:24
  • Ok, I've changed the target url to be a https, but still not getting the json in my response, getting status code as 200 but the callback is hitting the error part, not the success part. I've also disabled the security.mixedcontent feature of firefox. – Badhon Jain Aug 23 '16 at 06:31

1 Answers1

0

Would you mind trying this:

var datastring = { "trackNo" : "160822000037" };//try this
//var datastring = "trackNo=160822000037"; //or this

$.ajax({
            type: 'POST',//you may try the GET type
            url: "https://172.16.201.14:801/api/apiclearing", 
            dataType: 'json',// try jsonp as well
            data: datastring,
            contentType: 'application/json',
            crossDomain: true, //newly added
            success: function(data, status, jqXHR) {
                        console.log(data);
                        console.log(status);
                        console.log(JSON.stringify(data));//to string
                        alert("Success");

            },
           error:function(jqXHR, textStatus, errorThrown) {
                            alert("Status:"+ textStatus + " Error:" + errorThrown);


         }

You may consider as well to add Access-Control-Allow-Origin to your server like Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com in php

c.k
  • 1,075
  • 1
  • 18
  • 35
  • Tried this actually, but no difference. It not giving any error, giving 200 but not finding the json in the response object. – Badhon Jain Aug 23 '16 at 06:48
  • I think there's a problem with the server you are requesting. How does your server respond? – c.k Aug 23 '16 at 06:55
  • It might be, but when I'm hitting the url directly, its returing the json, I tried a few online json validator to make sure the response is valid json, two of the three online validator said the json is ok. – Badhon Jain Aug 23 '16 at 06:58
  • No, its in .net 4.0. – Badhon Jain Aug 23 '16 at 07:10
  • 1
    @Badhon, Totally didnt try it on .net. I edited my code, can you try it? you can also try palying the code to look what was happening. – c.k Aug 23 '16 at 07:24
  • After using the updated code, its hitting the error callback funciton where textStatus is showing persererror. Does it mean the server giving invalid json? – Badhon Jain Aug 23 '16 at 08:00
  • Also when I'm going to network tab of firebug, its showing the json as the response! – Badhon Jain Aug 23 '16 at 08:01
  • After using jsonp, error callback function giving this alert, Status:parsererror Error:Error: jQuery21408468486226022623_1471939193170 was not called – Badhon Jain Aug 23 '16 at 08:06
  • 1
    I see, I have feeling that the server doesn't throw the desire json data. Try checking your server response.. – c.k Aug 23 '16 at 08:13
  • I recommend using json not jsonp. Have you tried switching this `var datastring = { "trackNo" : "160822000037" };` and `var datastring = "trackNo=160822000037";` – c.k Aug 23 '16 at 08:22