【问题标题】:ajax erro function not workingajax错误功能不起作用
【发布时间】:2017-06-02 01:24:12
【问题描述】:
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: Unexpected token (

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

【问题讨论】:

  • 错误必须在你的 ajax 函数中,你把它放在外面,更不用说未关闭的(

标签: javascript jquery ajax


【解决方案1】:

Error 函数是 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 ")
    }
  });
}

【讨论】:

    【解决方案2】:

    您的错误:function() 在 ajax 调用之外。将错误函数移动到 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")
        }
    
    
        });
    }

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多