jQuery Mobile使用PHP+AAJAX填充列表视图


jQuery Mobile populate listview with PHP + AJAX

我正试图用PHP端的内容填充jQueryMobile的列表视图。

load_info.php文件具有以下代码:

if(isset($_GET['type'])){
    switch($_GET['type']){
        case 'locale': 
            load_locale("localhost", "root", "", "testing");
            break;
    }   
}
function load_locale($host, $user, $pass, $db){
    $db = new mysqli($host, $user, $pass, $db);
    $db->set_charset("utf8");
    $query = $db->prepare("SELECT `desc` FROM locale");
    $query->execute();
    $query->bind_result($desc);
    $query->store_result();
    $rows = array();
    while($query->fetch()){
        $rows[] = array("value" => $desc);
    }
    $query->close();
    $db->close();
    return json_encode($rows);      
}

在index.php页面中,我有以下内容:

<div data-role="page" id="page-locale"> 
    <div data-role="header" data-theme="b" data-close-btn="right">
        <h1>Locale</h1>
    </div>
    <div data-role="content" id="content">  
        <ul id="listview_locale" data-role="listview" data-inset="true" data-filter="true" data-autodividers="true" data-filter-placeholder="Search..">
        </ul>
    </div>
</div>
<script type="text/javascript">
        $(document).on("pagebeforeshow", "#page-locale", function() {
            $(function(){
                var items = "";
                $.getJSON("load_info.php?type=locale",function(data){
                    $.each(data, function(index, item) 
                    {
                        items += "<li>" + item.value + "</li>";
                    });
                    $("#listview_locale").html(items); 
                    $("#listview_locale").trigger("change");
                    $("#listview_locale").trigger("refresh");
                });
            });
        });
</script>

尽管如此,当我单击按钮转到页面id#页面区域设置时,listview什么都没有。我没有收到任何错误,但也没有任何内容。

怎么了?

编辑:已解决代替

$.getJSON("load_info.php?type=locale", function(data){

必须是

$.getJSON("classes/load_info.php?type=locale", function(data){

而不是

load_locale("localhost", "root", "", "testing");

必须是

echo load_locale("localhost", "root", "", "testing");

试试这个

你的数组是多维数组,所以你必须转换内部数组太

  while($query->fetch()){
        $data = array("value" => $desc)   
        $rows[] = json_encode($data);
    }

编辑

如果你像这样申报

echo load_locale("localhost", "root", "", "testing");

添加exit;

代码应该是:

echo load_locale("localhost", "root", "", "testing"); 
exit;

如果你给break。。它将破坏switch语句,而不是停止整个过程

你也可以这样申报

if(isset($_GET['type'])){
    switch($_GET['type']){
        case 'locale': 
            echo load_locale("localhost", "root", "", "testing");
            break;
    }  
    exit; 
}