无法在jquery中获得PHP值

时间:2016-04-15 14:16:16

标签: javascript php jquery

我的Php代码;

$sql = "SELECT * FROM login_speak";
if ($result = mysqli_query($con, $sql))
{
    $resultArray = array();
    $tempArray = array();

    while($row = $result->fetch_assoc())
    {
               $tempArray[] = $row;
    }            
$resultArray = json_encode($tempArray);
echo $resultArray; 

}
mysqli_close($con);
?>

我的jquery代码;

 $.getJSON("url.php", function(data){
           $("ul").empty();
           $.each(data.result, function(){
              $("ul").append("<li>Name:"+this['Name']+"</li>");
              alert("kj");
           });    
    });

我已经完成了几件事,但没有任何效果。我的PHP是正确的,我猜脚本有问题。 请给我答案,做错了什么。 感谢

3 个答案:

答案 0 :(得分:2)

json_encode from php can be accessed easily in a Javascript varible There are many ways of doing this, however consider the simple way first if it works with the array you are sending. in your example - assuming jquery:

  var valu =  [];
    $.ajax(url: "url.php",
           success: function(data){
                  valu=data;
           });    

see: http://www.dyn-web.com/tutorials/php-js/json/array.php

works for numeric and associative arrays.

答案 1 :(得分:0)

我认为您希望将代码更改为类似的内容..

$.each(data.result, function(index,value){
              $("ul").append("<li>Name:"+value.name+"</li>");
              alert("kj");
           });  

答案 2 :(得分:0)

继续我会做什么。

 $.ajax({
    url: "url.php",
    type: "GET",
    dataType: 'json'
}).done(function (data) {
    $.each(codes, function(key,value){
             $("ul").append('<option value="' + key + '">' + key + ' - ' + value + '</option>');
    });
}).fail(function (jqXHR, textStatus, error) {
    alert("error message");
    console.log("error message: " + error);
});
相关问题