2014-10-18 53 views
0

基於此主題:Changing the content of div in Jquery Mobile我想使用php和json從mysql表中檢索數據,但在此操作中不顯示任何內容。從mysql數據庫的列表視圖中的結果

以下是所使用的信息:

json.php

$conexion = new mysqli("localhost", 'xxx', 'xxx', 'Operations'); 

$query = "SELECT * FROM bugs"; 

$result = mysqli_query($conexion,$query); 

while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) { 

    $id['projectid'] = $row['projectid']; 
    $id['status'] = $row['status']; 
    $id['severity'] = $row['severity']; 
    $id['title'] = $row['title']; 
    $id['creation_date'] = $row['creation_date']; 

    array_push($result_array,$id); 

} 

echo json_encode($result_array); 

的index.html

<!DOCTYPE html> 
<html> 
<head> 
<title>Bugs Administration</title> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.css" /> 
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script> 
<script> 
     $(document).live('pageinit',function (event) { 
      $.ajax({ 
       url: 'http://127.0.0.1/app/json.php', 
       data: "", 
       dataType: 'json', 
       success: function(data)   
        { 
        for (var i = 0; i < data.length; i++) { 
         $('#list').append("<li><b>Project ID: </b>"+ data[i].projectid + 
              "<b>Status: </b>"+ data[i].status+ 
              "<b>Severity: </b>"+ data[i].severity+ 
              "<b>Title: </b>"+ data[i].title+ 
              "<b>Creation Date: </b>"+ data[i].creation_date+ 
             "</li>"); 
        } 
        $('#list').listview('refresh'); 

       } 
      }); 
     }); 
    </script> 
</head> 
<body> 
<div data-role="page" id="bugs"> 
    <div data-role="header"> 
     <h1>List of Bugs</h1>  
    </div><!-- /header --> 
    <div class="Main" data-role="content"> 
     <h3>Current opened bugs</h3>  
     <ul data-role="listview" id="list"></ul> 
    </div><!-- /content --> 
    <div data-role="footer"> 
     <h3>Mobile App</h3> 
    </div><!-- /footer --> 
</div><!-- /page --> 
</body> 
</html> 

其實是這樣的結果:

enter image description here

我做錯了什麼?

+0

什麼在控制檯(在大多數瀏覽器的F12)的Ajax調用的回報? – 2014-10-18 19:56:06

+0

Uncaught TypeError:無法讀取null的屬性'length'index.html:17 $ .ajax.success index.html:17 – m4g4bu 2014-10-18 20:09:26

+0

您可以在'echo json_encode($ result_array)'之前'var_dump($ result_array);'和檢查調用是否返回控制檯瀏覽器中的行(如果使用chrome,它將位於網絡面板中)? – 2014-10-18 20:14:48

回答

0

我認爲這是一個跨域問題,如果您的index.html文件與您的json.php位於相同的文件夾中,您可以嘗試使用相對路徑進行ajax調用嗎?

$.ajax({ 
    url: 'json.php', 
    data: "", 
    dataType: 'json', 
    success: function(data) 
    { 
     for (var i = 0; i < data.length; i++) { 
      $('#list').append("<li><b>Project ID: </b>"+ data[i].projectid + 
       "<b>Status: </b>"+ data[i].status+ 
       "<b>Severity: </b>"+ data[i].severity+ 
       "<b>Title: </b>"+ data[i].title+ 
       "<b>Creation Date: </b>"+ data[i].creation_date+ 
       "</li>"); 
     } 
     $('#list').listview('refresh'); 

    } 
}); 

下面是完整的index.html代碼:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Bugs Administration</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.css" /> 
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script> 
    <script src="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script> 
    <script> 
     $(document).live('pageinit',function (event) { 
      $.ajax({ 
       url: 'json.php', 
       data: "", 
       dataType: 'json', 
       success: function(data) 
       { 
        console.log(data); 
        for (var i = 0; i < data.length; i++) { 
         $('#list').append("<li><b>Project ID: </b>"+ data[i].projectid + 
          "<b>Status: </b>"+ data[i].status+ 
          "<b>Severity: </b>"+ data[i].severity+ 
          "<b>Title: </b>"+ data[i].title+ 
          "<b>Creation Date: </b>"+ data[i].creation_date+ 
          "</li>"); 
        } 
        $('#list').listview('refresh'); 

       } 
      }); 
     }); 
    </script> 
</head> 
<body> 
<div data-role="page" id="bugs"> 
    <div data-role="header"> 
     <h1>List of Bugs</h1> 
    </div><!-- /header --> 
    <div class="Main" data-role="content"> 
     <h3>Current opened bugs</h3> 
     <ul data-role="listview" id="list"></ul> 
    </div><!-- /content --> 
    <div data-role="footer"> 
     <h3>Mobile App</h3> 
    </div><!-- /footer --> 
</div><!-- /page --> 
</body> 
</html> 
+0

我添加了此警報(數據),應用程序響應爲空,因此調用出現問題 – m4g4bu 2014-10-18 20:38:08

+0

您的json.php文件與index.html位於同一文件夾中檔案? – 2014-10-18 20:39:53

+0

是的,兩者都存在 – m4g4bu 2014-10-18 21:06:55

相關問題