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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器