2014-01-11 80 views
1

我得到undefined当我运行此代码时,提示什么似乎是造成这个问题?越来越UNDEFINED值AJAX JSON

这是我insert_home.php

else if(isset($_POST['SELECTSLIDE'])) 
{ 
$SSID = ($_POST['SELECTSLIDE']); 
$result = mysqli_query($con,"SELECT *FROM slider SLIDE_NUM=('$SSID')"); 
while($row = mysqli_fetch_array($result)){ 

echo json_encode(array("a" => $row['SLIDE_TITLE'])); 

} 
mysqli_close($con); 
} 

,这是我的javascript

function SLIDE_EDIT(SLIDEID) 
{ 

$.post('insert_home.php',{SELECTSLIDE:SLIDEID}).done(function(data){ 
    alert(data.a); 
    }); 

} 
+0

你做了什么来调试呢?您是否检查过HTTP响应(使用浏览器的开发人员工具的“网络标签”)来查看它是否符合您的期望?数据是否正确?内容类型是否正确? – Quentin

回答

2

AJAX JSON格式不对,你应该指定查询获取导致阵列和使用json_encode功能后返回有效JSON 与此类似

else if(isset($_POST['SELECTSLIDE'])) 
    { 
    $response = array(); 
    $SSID = ($_POST['SELECTSLIDE']); 
    $result = mysqli_query($con,"SELECT *FROM slider SLIDE_NUM=('$SSID')"); 
    while($row = mysqli_fetch_array($result)){ 
     array_push($response, array("a" => $row['SLIDE_TITLE'])); 
    } 
    header('Content-type: text/json'); 
    echo json_encode($response); 
    mysqli_close($con); 
    } 

而在Ja vaScript,使用for循环来获得值,如

function SLIDE_EDIT(SLIDEID) 
{ 

$.post('insert_home.php',{SELECTSLIDE:SLIDEID}).done(function(data){ 
    for(var i in data){ 
     alert(data[i].a); 
     } 
    }); 

} 
+0

你也应该'exit()'或'die()',除非你很高兴JSON的有效性在输出后会有些不适。 – Popnoodles

+0

非常感谢! – user3184682

+0

Right @Popnoodles,exit()或者die()会很有用,谢谢 – Girish