Calling Google Ajax Search API via JQuery JSONP

后端 未结 2 1385
渐次进展
渐次进展 2021-01-03 05:34

I know this has been asked a zillion times, but I still can\'t get my code to work. I am trying to a simple JSONP call from my Javascript application. The cod fragment loo

相关标签:
2条回答
  • 2021-01-03 06:03

    Check this out JsFIddleDemo

        /*
         * create callbak function for jsonP
         * @params
         * data is response from http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=myjsonpfunction
         */
          function myjsonpfunction(data){
               alert(data.responseData.results) //showing results data
               $.each(data.responseData.results,function(i,rows){
                  alert(rows.url); //showing  results url
               });
          }
    
        //request data using jsonP
        $(function(){
            $.ajax({
            url:'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=myjsonpfunction',
            type:"GET",
            dataType: 'jsonp',
            jsonp: 'myjsonpfunction',
            async:'true',
            success:function (data) {
                //alert("success");
              }
            });
          });
    

    you need write a callback parameter and callback function,the google ajax apis will be return only json if you don't set of callback.

    if you set url like this

    http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=?(another)
    

    the response is

    {"responseData": null, "responseDetails": "bad or missing callback or context", "responseStatus": 400}

    0 讨论(0)
  • 2021-01-03 06:11

    Looks like the method you are using is deprecated: https://developers.google.com/web-search/docs/reference

    And has moved on to: http://code.google.com/apis/customsearch/v1/overview.html

    0 讨论(0)
提交回复
热议问题