Javascript jquery获取带超时的json

Javascript jquery获取带超时的json,javascript,jquery,json,Javascript,Jquery,Json,。这是我的java脚本的代码。我想在使用ajax调用json时为我的函数添加错误和超时…我该怎么做..我是新手..请提供帮助..我被卡住了 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="format-detection" content="telephone=no" /> <!--

。这是我的java脚本的代码。我想在使用ajax调用json时为我的函数添加错误和超时…我该怎么做..我是新手..请提供帮助..我被卡住了

<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link href="js/jquery.mobile-1.4.2.min.css" rel="stylesheet" type="text/index.css"/> 
        <title>Hello World</title>

        <script type="text/javascript" src="phonegap.js"></script>

        <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.4.2.min.js"></script>
        <script>
        $('#reposHome').bind('pageinit', function(event) {
            loadRepos();
        });

        function loadRepos() {
            $.ajax("https://api.github.com/legacy/repos/search/javascript").done(function(data) {
                var i, repo;
                $.each(data.repositories, function (i, repo) {
                    $("#allRepos").append("<li><a href='https://github.com/" + repo.username + "/" + repo.name + "'>"
                    + "<h4>" + repo.name + "</h4>"
                    + "<p>" + repo.username + "</p></a></li>");
                });
                $('#allRepos').listview('refresh');
                setTimeout(function(){
                    loadRepos();
                 }, 5000);
          })
          .fail(function (xhr, ajaxOptions, thrownError) {
                 alert(xhr.statusText);
                 alert(xhr.responseText);
                 alert(xhr.status);
                 alert(thrownError);
            });

        }
        </script>
    </head>
    <body>
    <div data-role="page" id="pagetwo">

    <div data-role="header">

    <h1>My First page</h1>
    </div>
    <div data-role="main" class="ui-content">
    <p>Welcome to My page</p><br>

  <ul id="allRepos" data-role="listview"  data-filter="true">
        </ul>
    </div>
    <div data-role="footer">
    <h1>Footer Text</h1>
  </div>
</div> 

    </body>
</html>

你好,世界
$('#reposome').bind('pageinit',函数(事件){
loadRepos();
});
函数loadRepos(){
$.ajax(”https://api.github.com/legacy/repos/search/javascript)。完成(函数(数据){
var i,回购;
$.each(数据存储库、函数(i、repo){
美元(“#所有回购协议”)。追加(“
  • ”); }); $('#allRepos')。列表视图('refresh'); setTimeout(函数(){ loadRepos(); }, 5000); }) .fail(函数(xhr、ajaxOptions、thrownError){ 警报(xhr.statusText); 警报(xhr.responseText); 警报(xhr.状态); 警报(thrownError); }); } 我的第一页 欢迎来到我的页面


    页脚文本

    这里我还添加了代码的html部分。

    您可以添加失败方法来执行错误块代码

     function loadRepos() {
            $.ajax({
                url: "https://api.github.com/legacy/repos/search/javascript",
                timeout: 1000, //change it to any value you want in milliseconds
            })
            .done(function(data) {
                var i, repo;
                $.each(data.repositories, function (i, repo) {
                    $("#allRepos").append("<li><a href='https://github.com/" + repo.username + "/" + repo.name + "'>"
                    + "<h4>" + repo.name + "</h4>"
                    + "<p>" + repo.username + "</p></a></li>");
                });
                $('#allRepos').listview('refresh');
                setTimeout(function(){
                      loadRepos();
                   }, 300000);
            })
            .fail(function (xhr, textStatus, thrownError) {
                   alert(xhr.statusText);
                   alert(xhr.responseText);
                   alert(xhr.status);
                   alert(thrownError);
            });
    }
    
    函数loadRepos(){
    $.ajax({
    url:“https://api.github.com/legacy/repos/search/javascript",
    超时:1000,//以毫秒为单位将其更改为所需的任何值
    })
    .完成(功能(数据){
    var i,回购;
    $.each(数据存储库、函数(i、repo){
    美元(“#所有回购协议”)。追加(“
  • ”); }); $('#allRepos')。列表视图('refresh'); setTimeout(函数(){ loadRepos(); }, 300000); }) .fail(函数(xhr、textStatus、thrownError){ 警报(xhr.statusText); 警报(xhr.responseText); 警报(xhr.状态); 警报(thrownError); }); }

    向ajax对象添加数据类型:“jsonp”

    然后,有效载荷会返回类似于

    "meta": {
        "X-RateLimit-Limit": "60",
        "X-RateLimit-Remaining": "58",
        "X-RateLimit-Reset": "1400066468",
        "Cache-Control": "public, max-age=60, s-maxage=60",
        "Vary": "Accept",
        "ETag": "\"4d8b8354d543d2739fd9dceb66f3b65e\"",
        "X-GitHub-Media-Type": "github.v3",
        "status": 200
      },
      "data": {
        "repositories": [
          {
            "type": "repo",
            "username": "airbnb",
            "name": "javascript",
            "owner": "airbnb",
            "homepage": null,
            "description": "JavaScript Style Guide",
            "language": "",
            "watchers": 9039,
            "followers": 9039,
            "forks": 1595,
            "size": 1694,
            "open_issues": 19,
            "score": 238.48807,
            "has_downloads": true,
            "has_issues": true,
            "has_wiki": true,
            "fork": false,
            "private": false,
            "url": "https://github.com/airbnb/javascript",
            "created": "2012-11-01T23:13:50Z",
            "created_at": "2012-11-01T23:13:50Z",
            "pushed_at": "2014-05-14T02:42:27Z",
            "pushed": "2014-05-14T02:42:27Z"
          },
    
    因此,您不能只使用
    数据.repositories
    它应该是
    有效负载.data.repositories

    function loadRepos() {
            $.ajax({
                url: "https://api.github.com/legacy/repos/search/javascript",
                timeout: 5000,
              dataType: 'jsonp'
    
            })
            .done(function(payload) {
                var i, repo;
               $.each(payload.data.repositories, function (i, repo) {
                    $("#allRepos").append("<li><a href='https://github.com/" + repo.username + "/" + repo.name + "'>" + "<h4>" + repo.name + "</h4>"+ "<p>" + repo.username + "</p></a></li>");
                });
                $('#allRepos').listview('refresh');
    
                setTimeout(function(){
                      loadRepos();
                   }, 300000);
            })
            .fail(function (xhr, textStatus, thrownError) {
                   alert(xhr.statusText);
                   alert(xhr.responseText);
                   alert(xhr.status);
                   alert(thrownError);
            });
    }
    
    函数loadRepos(){
    $.ajax({
    url:“https://api.github.com/legacy/repos/search/javascript",
    超时:5000,
    数据类型:“jsonp”
    })
    .完成(功能(有效载荷){
    var i,回购;
    美元。每个(有效载荷。数据。存储库,函数(i,repo){
    美元(“#所有回购协议”)。追加(“
  • ”); }); $('#allRepos')。列表视图('refresh'); setTimeout(函数(){ loadRepos(); }, 300000); }) .fail(函数(xhr、textStatus、thrownError){ 警报(xhr.statusText); 警报(xhr.responseText); 警报(xhr.状态); 警报(thrownError); }); }
    try我已经看到了……但我还没有被卡住……所以请告诉我如何在我的代码中添加此代码try this code@user3541465检查我的答案以了解代码的工作版本:)Thanx。它工作正常,但没有显示警报,也没有附加listview,,请告诉我问题出在哪里..检查网络并查看是否收到任何响应或错误。网络正常..但仍不工作..也没有错误..是的,有错误…白名单..对网络资源的访问不受限制..如何解决此问题?这将为我正确返回响应。你能创建一个JSFIDLE并共享链接吗?