gpt4 book ai didi

php - 从 mysql php jquery ajax 读取数据

转载 作者:可可西里 更新时间:2023-11-01 07:36:43 24 4
gpt4 key购买 nike

我需要使用 jquery + ajax 从我的网页读取数据

这是我的功能:

public function getvalueshahr()
{
if($_POST['ostan']!=0){
$db= JFactory::getDbo();
$query=$db->getQuery(TRUE);
$query->select('id,title')->from('#__categories')->
where($db->quoteName('parent_id').'='.$db->quote($_POST['ostan']));
$db->setQuery($query);
$res=$db->loadObjectList();
echo $db->getErrorMsg();
echo json_encode($res);}
}

我可以像这样用 C# 读取数据:

enter image description here

但是我的 ajax 方法不起作用。方法如下:

 $.ajax({
type: "POST",
url: "www.mysite.net/index.php?task=shahrestan.getvalueshahr",
data: "{ostan=77}",
dataType: "json",
success: function (result)
{
var d = $.parseJSON(result);
$.each(d, function (i, field)
{
$("#output").append("id: " + field.id + " title: " +
field.title+"br/>");
});
},
error: function (e)
{
alert("error:" + e.responseText);
}
});

该方法不返回任何内容。

最佳答案

您在 ajax 请求中发送的是字符串而不是对象。尝试改变:

data: "{ostan=77}"

data: { ostan: 77 }

在 PHP 中回显结果之前,您应该设置内容类型:

header('Content-Type: application/json');
echo json_encode($res);

现在您不需要在 JS 代码中使用 $.parseJSON。您将立即获得一个 json 对象。

关于php - 从 mysql php jquery ajax 读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36761003/

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