从JavaScript中获取Bing Search API的json数据

时间:2016-04-15 09:50:20

标签: javascript json bing-api bing-search

我试图使用Bing Search API返回json数据并显示它。 返回的json结果的格式为:

{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\u0027britain\u0027&$skip=0&$top=1","type":"NewsResult"},"ID":"38eda3ba-9e50-4d48-9fd1-bde42b743966","Title":"Britain\u0027s Prince William and Kate try archery in Bhutan","Url":"http://timesofindia.indiatimes.com/world/south-asia/Britains-Prince-William-and-Kate-try-archery-in-Bhutan/articleshow/51835828.cms","Source":"Times of India","Description":"Britain\u0027s Catherine looks on as she watches Prince William prepare to fire an arrow at an archery ground in Th... Read More THIMPHU: Britain\u0027s Prince William and his wife Kate tried their hand at Bhutan\u0027s national sport of archery on Thursday as they began ...","Date":"2016-04-15T08:30:00Z"}],"__next":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\u0027britain\u0027&$skip=15"}}

我试图做的是这个

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>

<script src="jquery-1.12.3.js">

$(document).ready(function() {

  var appId = ':MyKeyHere';

  function getNews() {
    var azureKey = btoa(appId);
    var myUrl = 'https://api.datamarket.azure.com/Bing/Search/v1/News?Query=%27britain%27&$format=json';

    $.ajax({
      method: 'post',
      url: myUrl,
      dataType:"json"

      //Set headers to authorize search with Bing
      headers: {
        'Authorization': 'Basic ' + azureKey
      },
      success: function(data) {
       var json = data.d.results[0].Title;    
       document.getElementById("demo").innerHTML = json;
      },
      failure: function(err) {
        console.error(err);  
      }
    });
  };

  getNews();

});
</script>
</body>
</html>

有谁能告诉我我做错了什么? 我是新手。所以,有几个问题:

  1. 数据变量是否包含json对象,还是包含json字符串,我需要解析它?

  2. 我没有输出也没有错误。我错过了什么?

0 个答案:

没有答案