ajax错误功能无法正常工作

时间:2017-06-01 17:18:51

标签: javascript jquery ajax

function getWikiData(marker)  {

    var wikiurl = "https://en.wikipedia.org/w/api.php?
action=opensearch&search=" +
      marker.title + "&format=json&callback=wikiCallback";

    $.ajax({

      url: wikiurl,
      dataType: "jsonp",
      // jasonp: "callback",
      success: function(response) {
        var summary = response[2][0];
        var article = response[3][0];
        var articleUrl = article;

        console.log(response);
        console.log(response[2][0]);
        console.log(response[3][0]);


        self.infoWindow.setContent('<h2>' + marker.title + '</h2><p>' + 
summary + '</p>' + '<a title="go to wikipedia article" href="' + articleUrl 
+ '">> go to wikipedia article</a>'); self.infoWindow.open(map, marker);
      }

    })


    error: function(){
          alert("An Error Occurred Loading Wikipedia Article. Please try 
again later")
    };



}

我正在尝试将错误功能添加到我的ajax

Console.log错误

script.js:255 Uncaught SyntaxError:意外的令牌(

第255行将是错误:function(error){ 不知道为什么它不会读我的代码。

2 个答案:

答案 0 :(得分:0)

错误函数是AJAX的一部分,因此它需要在其中。请尝试以下代码:

function getWikiData(marker) {

  var wikiurl = "https://en.wikipedia.org/w/api.php?
  action = opensearch & search = " +
  marker.title + "&format=json&callback=wikiCallback";

  $.ajax({

    url: wikiurl,
    dataType: "jsonp",
    // jasonp: "callback",
    success: function(response) {
      var summary = response[2][0];
      var article = response[3][0];
      var articleUrl = article;

      console.log(response);
      console.log(response[2][0]);
      console.log(response[3][0]);


      self.infoWindow.setContent('<h2>' + marker.title + '</h2><p>' +
        summary + '</p>' + '<a title="go to wikipedia article" href="' + articleUrl +
        '">> go to wikipedia article</a>');
      self.infoWindow.open(map, marker);
    },
    error: function() {
      alert("An Error Occurred Loading Wikipedia Article. Please try again later ")
    }
  });
}

答案 1 :(得分:0)

您的错误:function()超出了ajax调用。将错误函数移到ajax调用中

&#13;
&#13;
function getWikiData(marker)  {

    var wikiurl = "https://en.wikipedia.org/w/api.php?action=opensearch&search=" + marker.title +"&format=json&callback=wikiCallback";

    $.ajax({

      url: wikiurl,
      dataType: "jsonp",
      // jasonp: "callback",
      success: function(response) {
        var summary = response[2][0];
        var article = response[3][0];
        var articleUrl = article;

        console.log(response);
        console.log(response[2][0]);
        console.log(response[3][0]);


        self.infoWindow.setContent('<h2>' + marker.title + '</h2><p>' + 
summary + '</p>' + '<a title="go to wikipedia article" href="' + articleUrl 
+ '">> go to wikipedia article</a>'); self.infoWindow.open(map, marker);
      },
       error: function(){
          alert("An Error Occurred Loading Wikipedia Article. Please try again later")
    }


    });
}
&#13;
&#13;
&#13;