简体   繁体   English

Ajax错误功能不起作用

[英]ajax erro function not working

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")
    };



}

I'm trying to add error function to my ajax 我正在尝试向我的ajax添加错误功能

Console.log error Console.log错误

script.js:255 Uncaught SyntaxError: Unexpected token ( script.js:255未捕获的SyntaxError:意外令牌(

line 255 would be error:function(error){ not sure why it won't read my code. 255行将是error:function(error){不知道为什么它不会读取我的代码。

Error function is part of the AJAX and hence it needs to be inside it. 错误功能是AJAX的一部分,因此必须包含在其中。 Try this below code: 试试下面的代码:

 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 ") } }); } 

Your error:function() was outside of ajax call. 您的错误:function()在ajax调用之外。 move error function to inside ajax call 将错误函数移到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") } }); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM