2017-04-23 115 views
0

我用Javascript,JQuery和ajax编写的测试程序有点问题。JSON解析问题jquery,ajax

这是我的代码:

HTML

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Index</title> 
    <script src="https://code.jquery.com/jquery-2.1.1.js"></script> 
    <script src="script.js"></script> 
</head> 
<body> 
<button> Push here </button> 
</body> 
</html> 

JS

$(document).ready(function(){ 
    $('button').on('click',function(e){ 
     callPage() 
    }) 
}); 


function callPage(){ 
    $.ajax({ 
     url: "http://events.restdesc.org/", 
     type: "GET", 
     dataType:'json', 
     succes: function (response) { 
      var data = response; 
      console.log("hey"); 
      console.log(data.title); 
     }, 

     error: function(error){ 
      console.log("the page was not loaded", error); 
     }, 

    }); 
} 

但是如果我检查网络,我看到我的要求,但 我不获得成功,所以控制台不记录任何东西。 这怎么可能?

+6

错字在 “成功”? – joaofs

+0

哦不,我一直在寻找这个好几个小时,这并没有显示webstorm中的任何错误 – fangio

+0

@fangio在“成功”中的错字仍然会产生错误,即使他们没有被记录。 –

回答

-1

使用.done()而不是成功

function callPage(){ 
    $.ajax({ 
     url: "http://events.restdesc.org/", 
     type: "GET", 
     dataType:'json', 


     error: function(error){ 
      console.log("the page was not loaded", error); 
     }, 

    }).done(function(response) { 
       var data = response; 
      console.log("hey"); 
      console.log(data.title); 
    }); 
} 
+0

对于捕获方法名称中的拼写错误,这实际上是一种更好的方法。 – Phil