从数据库通过AJAX获取JSON

时间:2016-02-20 16:16:50

标签: javascript php jquery json ajax

我的代码无效。谁能告诉我它有什么问题?我从这段代码中得不到任何结果。请帮忙。

<a href="" id="navigation-edit">EDIT MENU</a>   
$(function(){
    $("a#navigation-edit").on(("click"),function(){ 
         $.getJSON("ajax/navigation-edit.php", function(data) {
             $("#result").html(data);
        });              
    });
});
  

导航-edit.php

 <?PHP

$result_NavDel = $mysqli->query("SELECT * FROM navigation_dynamic");
while($row = $result_NavDel->fetch_assoc()){
    $data[] = "<li class='animated bounce infinite'><a  href='?".$row['url']."'>".$row['menu']."</a></li>";

}

$implode='<ul class="nav navbar-nav" id="deleteNav">'`.`implode($data).'</ul>';
 echo json_encode($implode);




 ?>

1 个答案:

答案 0 :(得分:0)

当您使用vector时,您正在请求JSON格式的响应。

然而,从你的php和回调代码的意图来看,你只需要$.getJSON()返回,所以只需从使用text/html更改为使用$.getJSON(),默认为$.get()

如果你确实想要JSON,那么改为:

PHP

text/html

并将`$ .getJSON的回调更改为:

$data=array('text'=>'OK');
echo json_encode($data);