weixin_33728268 2013-08-09 06:00 采纳率: 0%
浏览 12

$ .ajax()不接受php数组

I'm using ajax to return results from a table (mysql). Ajax calls are made using jquery. Databases are accessed using PDOs.

PHP

    function fetchQuestions() {
            $database = "answerMe";  // the name of the database.
            $server = "127.0.0.1";  // server to connect to.
            $db_user = "name";  // mysql username to access the database with.
            $db_pass = "password";  // mysql password to access the database with.
            $table = "questions";
            $result = "";

            try {
              $testh = new PDO ("mysql:host=$server;dbname=$database", $db_user, $db_pass);
              $testh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
              $testh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
              $sql = "select COUNT(*) from questions";
              $pstatement = $testh->prepare($sql);
              $success = $pstatement->execute();
              $num = $pstatement->fetchColumn();
              $sql = "select * from $table";
              $pstatement = $testh->prepare($sql);
              $success = $pstatement->execute();
              print("Fetch all of the remaining rows in the result set:
");
              $result = $pstatement->fetchAll(PDO::FETCH_COLUMN, 1);
              return $result;
            }
            catch(PDOException $e)
            {
              echo "Following error was encountered <br />";
              echo $e->getMessage();
            }
    }
    $function = $_POST['function'];    
    $response = array();
    $data = "";

    switch($function) {
             .
             .
             .      

             case('recData'):
                //$qs = array('w','k');
                $qs = fetchQuestions();
                $response['records'] = $qs;
                    break;
    }    
    echo json_encode($response);

Jquery(ajax part)

    function AjaxReq(){
            this.sendSuccess = false;
            this.recSuccess = false;
            this.send = sendData;
            this.rec = recData;
    }
    .
    .
    .
    function recData() {
            $.ajax({
               type: "POST",
               url: "ajax.php",
               data: {  'function': 'recData',
                                    },
               dataType: "json",

               success: function(data){
                       alert('rec succeeded');
                       this.recSuccess = true;
                       for(var i = 0; i < data.records.length; ++i) {
                                    $("#details").append(data.records[i]);
                            }
               },
            });
    }

Jquery (calling ajax)

    $(document).ready(function(){
            var reqTest = new AjaxReq();
            $("#done").bind('click',function(){
                    reqTest.rec();
            });
    });

In the PHP code, case 'recData', if I uncomment //$qs = array('w','k');, I get the results.

This made me conclude maybe $qs wasn't an array probably. But using is_array(), its was proven otherwise.

I have tried my best to keep irrelevant code away from here. Any help would be appreciated. Thank you.

  • 写回答

3条回答 默认 最新

  • 七度&光 2013-08-09 07:22
    关注

    try to use this java script var_dump library to debag Your data response. https://github.com/kvz/phpjs/blob/master/functions/var/var_dump.js

    评论

报告相同问题?

悬赏问题

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