从mysql php jquery ajax读取数据

从mysql php jquery ajax读取数据,php,jquery,mysql,ajax,post,Php,Jquery,Mysql,Ajax,Post,我需要使用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').'='

我需要使用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#读取数据,如下所示:

data: {ostan: 77},
$.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);
  }
 });

但是我的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}"

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

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

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

首先,您向服务器发送了一个错误的数据。您必须重写部分代码,如下所示:

data: {ostan: 77},
$.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);
  }
 });

第二个问题是,您在success方法上获得一个结果作为json对象,但您试图再次将其解析为json对象。因此,请从代码中删除这一行:

var d = $.parseJSON(result);
最后,ajax函数应该如下所示:

data: {ostan: 77},
$.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);
  }
 });

如果您正在从mysql数据库加载数据,则最终必须转换这些数据,因为这些数据不是以utf-8转换的形式存储的。这段代码将给定对象转换为UTF-8:
mb_convert_编码(数据库条目,'UTF-8')

你应该真正学会如何编写好的代码,你的代码看起来很凌乱,很难阅读。谢谢任何书籍或电子书。你的意思是什么?谢谢,它会返回一条错误消息,而e.responseText是空的。我如何获取错误详细信息向ajax添加一个“完整”方法,以获取有关服务器错误的所有数据。你必须这样写:complete:function(res){console.log(res);}在chrome浏览器上运行你的代码,而不是从控制台复制并粘贴输出结果pleasethanks,它返回一条错误消息,e.responseText为空。如何获取错误详细信息如果你尝试
console.log(e)
而不是
alert(e.responteText)
捕获错误时?使用浏览器控制台查看输出。以下是如何在不同浏览器中打开它: