0

i am using below to get json response. i didn't get any output. Please find the error.

<script>
$.ajax({ 
    url : "https://allbills.in/api/operator-detail",
    type : 'GET',
    data: {  
       id: "41263087",
       token: "4MCquK7ro2HVXQ8zjwmIPs5vBlg9RbDpYSaFtLiy",
       mobile:"9739733333" 
    }
    dataType : 'json',
    contentType : 'application/json',
    success : function(data) {
        console.log(JSON.stringify(data));
        alert(JSON.stringify(data));
    },
    error : function() {
        console.log("Cannot get data");
    }
    alert("hi");
});
</script>

jsfiddle link please check here

Venkatesh Panabaka
  • 2,064
  • 4
  • 19
  • 27
  • where is your `curl`? you are calling `ajax` as `curl`? –  Feb 17 '17 at 09:54
  • yes. i calling ajax as curl – Venkatesh Panabaka Feb 17 '17 at 09:57
  • If you check the console you have a couple of syntax problems (a missing `,` and `alert()` in the wrong place). When you fix those issues (https://jsfiddle.net/32pzuwfs/) you then get this error in the console: `Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource`. This means that the domain you're calling does not returns CORS headers in the response, hence you cannot make a request to it through JS code. You will have to do it server side. – Rory McCrossan Feb 17 '17 at 09:57
  • @VenkateshPanabaka, use `$_POST` instead of `$_GET` then you will receive this response `{"pastie_url_relative": "/32pzuwfs/1/", "slug": "32pzuwfs", "shell_id": 293074474}` –  Feb 17 '17 at 10:11
  • @EaBangalore where your getting response. can you give me your modifed code link or code – Venkatesh Panabaka Feb 17 '17 at 10:17
  • @VenkateshPanabaka,the above `response` is not `correct`? –  Feb 17 '17 at 10:27
  • @VenkateshPanabaka,is that response is correct or not? –  Feb 17 '17 at 10:36
  • @EaBangalore response is wrong. you got something. i didn't get any response. Because of that i asked your code – Venkatesh Panabaka Feb 17 '17 at 10:38
  • @VenkateshPanabaka EaBangalore just said to replace "GET" with "POST" in your code, that's all. Also you need to make the changes suggested by Rory before the code will even compile. But even after all that, you still have the fundamental problem that the API doesn't accept CORS requests. This is beyond your control, unless the API is maintained by you as well? You will have to do this via curl in PHP. See this fiddle: https://jsfiddle.net/xar3jb1o/5/ and check the browser console – ADyson Feb 17 '17 at 11:33

0 Answers0