我需要使用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#读取数据:
$.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);
}
});
该方法什么都不返回.
解决方法:
首先,您将错误的数据发送到服务器.您必须像这样重写部分代码:
data: {ostan: 77},
要么
data: "ostan=77",
第二个问题是你在success方法上得到一个json对象的结果,但是你尝试再次将它解析为json对象.所以从你的代码中删除这一行:
var d = $.parseJSON(result);
最后你的ajax函数应如下所示:
$.ajax({
type: "POST",
url: "www.mysite.net/index.PHP?task=shahrestan.getvalueshahr",
data: {ostan: 77},
dataType: "json",
success: function (result)
{
$.each(result, function (i, field)
{
$("#output").append("id: " + field.id + " title: " +
field.title+"br/>");
});
},
error: function (e)
{
alert("error:" + e.responseText);
}
});