gpt4 book ai didi

javascript - AJAX json 返回未定义

转载 作者:行者123 更新时间:2023-11-28 20:35:05 25 4
gpt4 key购买 nike

我有简单的ajax查询

$.ajax(
{
url:'ADMPANEL_POST.php',
type:'POST',
data:{'z':'1'},
dataType:'json',
success:function (result) {
alert( "Data recieved: " + result.x );
$('#result').append(result.x);
}
});

在 ADMPANEL_POST.php 中,我尝试获取 z=1 并拥有适当的处理程序,并且我希望使用 json_encode 将 data_str 返回到 ajax 成功函数

if(isset($_POST['z'])) 
{
$init_x = 'test';
$data_str[] = array('x' => $init_x);
if($_POST['z']=='1') {
echo json_encode($data_str, true);
}
}

但是我在警报消息中收到了 data.x 的未定义值。无法弄清楚是什么问题。谢谢。P.S.:看到了这个,但没有帮助JSON returning as undefined

最佳答案

您的格式错误,如果您想要result.x == 'test',请使用$data_str = array('x' => $init_x);

关于javascript - AJAX json 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15590607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com