Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery ajax get json data

how to get image url from "https://api.qwant.com/api/search/images?count=10&offset=1&q=cars" api using jquery. i'm unable to do it. bellow i've attached my code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$.ajax({
    url:"https://api.qwant.com/api/search/images?count=10&offset=1&q=cars",
    type:"GET",
    crossDomain : true,
    async: false,
    dataType: "jsonp",
    jsonpCallback: "myJsonMethod",
    success: function(json) {
       $.parseJson(json)
    },
    error: function(e) {
       console.log(e);
    }
});

function myJsonMethod(response)
{
    console.log(response);
}
</script>
like image 567
Abhijit Das Avatar asked Oct 28 '17 10:10

Abhijit Das


Video Answer


1 Answers

Your request isn't working because of CORS, which is enabled on the API server. You need a proxy server to workaround this. For development purposes you could use a free online proxy server, your code then simplifies:

 $.ajax({
    url:"<PROXY:SERVER>https://api.qwant.com/api/search/images?count=10&offset=1&q=cars",
    success: function(json) {
        // Do stuff with data
    },
    error: function(e) {
       console.log(e);
    }
});

As an example check out this working fiddle.

like image 68
Andrei Roba Avatar answered Sep 17 '22 09:09

Andrei Roba