weixin_33721344 2017-08-06 08:10 采纳率: 0%
浏览 8

如何执行Ajax请求?

Am trying to make wikipedia viewer for my freecodecamp project. But the ajax request fails every time. It does not return anything.

var url, value;
$(document).ready(function() {
  $("button").on("click", function() {
    value = $("input").val();
    var url = "https://en.wikipedia.org/w/api.php?action=opensearch&search=" +
      value + "&format=json&callback=?";
    $.ajax({
      type: "GET",
      url: url,
      async: false,
      dataType: "json",
      //jsonp: "callback",
      success: function(data) {
        console.log(data);
      }
    });
  });
});

</div>
  • 写回答

2条回答 默认 最新

  • weixin_33701294 2017-08-06 08:18
    关注
    1. set dataType: 'jsonp'
    2. remove &callback=? from the url (that's the default that jQuery will use anyway

    example

    var value = "google";
    var url = 'https://en.wikipedia.org/w/api.php?action=opensearch&search='+ value + '&format=json';
    $.ajax({
        type: 'GET',
        url: url,
        dataType: 'jsonp',
        success: function (data)
        {
            console.log(data);
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    </div>
    
    评论

报告相同问题?