首次加载页面后,json数据不会加载

时间:2015-07-18 09:43:11

标签: javascript jquery html json

我想将JSON数据读入JavaScript,但只有在页面刷新后才会在初始页面加载时显示数据。

这是我的代码:

$(document).ready(function () {
  $.ajax({
            url:"http://192.168.0.105/stratagic-json/project_json.php",
            type:"GET",
            dataType:"json",
            beforeSend: function(){
            success:function(jsonData){
     var projctList = '';

  for (var i = 0; i < jsonData.length; i++) {
      projctList += ' <li><div class="proj-details-wrap"> <img src="images/project-img.jpg" /><div class="proj-badge">Upcoming Projects</div><div class="proj-name">'+ jsonData[i].name +'<span>'+ jsonData[i].location +'</span> </div><div class="proj-status">'+ jsonData[i].percentage +'% <span>completed</span> </div></div><div class="container proj-desc">'+ jsonData[i].description +' </div> </li>';
     }
          $("#projctLists").html(projctList);

     }
     }); 
});
  <ul class="slides" id="projctLists">
        
            </ul>

3 个答案:

答案 0 :(得分:0)

发现一个错误,成功在beforeSend,Corrected并在下面显示

    $(document).ready(function () {
        $.ajax({
            url:"http://192.168.0.105/stratagic-json/project_json.php",
            type:"GET",
            dataType:"json",
            beforeSend: function(){ },
                success:function(jsonData){
                    var projctList = '';

                    for (var i = 0; i  Upcoming Projects'+ jsonData[i].name +''+ jsonData[i].location +' '+ jsonData[i].percentage +'% completed '+ jsonData[i].description +'  ';
                    }
                    $("#projctLists").html(projctList);

                }
            });
    });

答案 1 :(得分:0)

您需要从代码中删除以下行

 beforeSend: function() {

答案 2 :(得分:0)

beforeSend: function() {附近有语法错误 使用您的代码,如

$(document).ready(function () {
        $.ajax({
            url:"http://192.168.0.105/stratagic-json/project_json.php",
            type:"GET",
            dataType:"json",
            beforeSend: function(){ },
                success:function(jsonData){
                    var projctList = '';

                   for (var i = 0; i < jsonData.length; i++) {
  projctList += ' <li><div class="proj-details-wrap"> <img src="images/project-img.jpg" /><div class="proj-badge">Upcoming Projects</div><div class="proj-name">'+ jsonData[i].name +'<span>'+ jsonData[i].location +'</span> </div><div class="proj-status">'+ jsonData[i].percentage +'% <span>completed</span> </div></div><div class="container proj-desc">'+ jsonData[i].description +' </div> </li>';
 }
                    $("#projctLists").html(projctList);

                }
            });
    });