Php 获取未定义的值ajaxjson
当我运行此代码时,Php 获取未定义的值ajaxjson,php,jquery,html,mysql,ajax,Php,Jquery,Html,Mysql,Ajax,当我运行此代码时,undefined发出警报,是什么导致了问题 这是我的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
undefined
发出警报,是什么导致了问题
这是我的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);
});
}
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);
}
在JavaScript中,使用for循环获取值,如
function SLIDE_EDIT(SLIDEID)
{
$.post('insert_home.php',{SELECTSLIDE:SLIDEID}).done(function(data){
for(var i in data){
alert(data[i].a);
}
});
}
你做了什么来调试这个?您是否已检查HTTP响应(使用浏览器的“开发人员工具”的“网络”选项卡)以查看它是否符合预期?数据正确吗?内容类型正确吗?您也应该
exit()
或die()
,除非您很高兴在输出JSON后有什么东西会破坏JSON的有效性。right@poplane,exit()或die()会很有用,谢谢您@user3184682Walanjo ang GALENG!哈哈