douhuan5073 2013-02-12 20:18
浏览 109
已采纳

jquery ajax调用mysql

I have found many threads regarding this issue, but unfortunately I couldn't get it running. Problem is I don't know much about JQuery.

I am trying to make an Ajax call using JQuery in order to fetch multiple records from a mysql database. I have the following function :

function updateWebpage () 
{
$.ajax({                                      
url: './sale/api.php',                  //the script to call to get data          
data: "",                  //you can insert url argumnets here to pass to  api.php
                                   //for example "id=5&parent=6"
  dataType: 'json',                //data format      
  success: function(rows)          //on recieve of reply
  {
        for (var i in rows)
        {
          var row = rows[i];          

          var username = row[0];
          var stateId = row[1];
          $('#output').append("<b>id: </b>"+username+"<b> stateId: </b>"+stateId)
                      .append("<hr />");
        } 
    } 
});

};

My api.php is executing a mysql query with something like this:

$array = retrieveUsersInfo('%');                          //fetch result    
echo json_encode($array);

My main issue, is how to debug an issue like this? Since ajax is calling asynchronously another file, I cannot view any errors. From my firefox debugger, I can see that the $.ajax function is entered, but success is not.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongliao3742 2013-02-12 20:22
    关注

    a couple things to try.

    1. hit the api url directly in a browser (not through ajax) and make sure it returns the valid response.
    2. add an error: function(err){} to your jquery ajax call. this method will get called if there is something other than a 200 response back from the server.
    3. I use Chrome's developer tools more than firefox/firebug. It has a Network tab in it that shows me all the communication between the client and the server. You should see a call out to your api in that tab.

    just off hand, i think you need to make sure the mime-type is set to text/json in your php file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配