javascript - JQuery ajax get json data - Stack Overflow

how to get image url from ";offset=1&q=cars" api using jquery. i'm unable to do it.

how to get image url from ";offset=1&q=cars" api using jquery. i'm unable to do it. bellow i've attached my code

<script src=".2.1/jquery.min.js"></script>
<script>
$.ajax({
    url:";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>

how to get image url from "https://api.qwant./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./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$.ajax({
    url:"https://api.qwant./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>
Share Improve this question asked Oct 28, 2017 at 10:35 Abhijit DasAbhijit Das 1052 gold badges3 silver badges6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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./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.

Try this

$.ajax({
    url:"https://api.qwant./api/search/images?count=10&offset=1&q=cars",
    type:"GET",
    crossDomain : true,
    async: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    jsonpCallback: "myJsonMethod",
    success: function(json) {
       $.parseJson(json)
    },
    error: function(e) {
       console.log(e);
    }
});

This is a CORS thing, so you can server all this up from a web server, like http-server, and I think certain browsers like Firefox allow this to occur locally.

There are some flags with Chrome that'd allow it to work locally like this too, I believe.

Cheers

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743726466a4496700.html

相关推荐

  • javascript - JQuery ajax get json data - Stack Overflow

    how to get image url from ";offset=1&q=cars" api using jquery. i'm unable to do it.

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信