使用ajax调用返回json记录时解析错误

时间:2013-11-21 19:24:15

标签: php json jquery

我仍然是了解MVC,Ajax概念以及它们如何协同工作以从数据库中调用数据的新手,请耐心等待:)

我遇到了以下错误:

  

解析错误语法错误意外的令牌< [对象,对象]

当我从数据库中返回值时。

我不确定:

  1. 我是否正确地将数据从ajax传递到要解析的查询,因为我使用了bindParam以便变量可以正确发布,但我没有看到任何结果生成。
  2. 我是否正确格式化了ajax调用?
  3. 服务器端

    quiz_controller.php

    switch($_POST['action']) {
    case 'get_users':
        print $users->getUsers($questID);
    break;
    }
    

    Quiz.php

    $questID = $db->$_POST['qid'];
    
    class Quiz {
    
    private $dbh;
    
    public function __construct($host,$user,$pass,$db)  {       
        $this->dbh = new PDO("mysql:host=".$host.";dbname=".$db,$user,$pass);       
    }
    
    //getQuestion : To get single questions related fields
    public function getUsers(){             
        //$sth = $this->dbh->prepare("SELECT a.questID, a.questTitle, a.questDesc, b.qCatName,c.qTypTitle FROM eq_question AS a INNER JOIN eq_question_category AS b ON a.qCatID = b.qCatID INNER JOIN eq_question_type AS c ON a.qTypID WHERE questID= $questID ");
        $sth = $this->dbh->prepare("SELECT a.questID, a.questTitle, a.questDesc, b.qCatName,c.qTypTitle FROM eq_question AS a INNER JOIN eq_question_category AS b ON a.qCatID = b.qCatID INNER JOIN eq_question_type AS c ON a.qTypID WHERE questID= :parameter ");
        //$sth->bindParam(':parameter', $questID, PDO::PARAM_STR);
        $sth->bindParam(':parameter', $questID);
        $sth->execute();
        return json_encode($sth->fetchAll());
    }
    

    客户端

    quiz_controller.js

    $(function() {  
        $(document).on("click", "button#ct", function(){ getUserList(this); }); 
    }
    
    function getUserList(element) {
        alert('worked!');
        $('#indicator').show();
        qid = 3;
        alert('worked assign qid    !');
        alert(qid+'is the qid');
            $.ajax({
                type: "POST",
                url: "../../equizz/app/controller/quiz_controller.php",
                dataType: "json",
                data: {
                 "questID":qid, //page: value in the url php  : currentpage: value in js 
                 //action: 'get_users'
                 action: 'get_users'
                },
                success: function(data){
                    alert('data now is '+data);
                    renderUserList(data);
                    $('#indicator').hide();
                },//success: function(data) END
                error: function(XMLHttpRequest, textStatus, errorThrown) { 
                    alert('error!');
                    alert(textStatus+"  " + errorThrown +"  " + XMLHttpRequest); 
                } //error END
    
            });//$.ajax() END
    }//getUserList END
    function renderUserList(jsonData) {
        //ACW- instead of generate a new table of content. Replace content inside related ID fields!
        var table = '</nav><!--Top Navigation END--><div class="content black"><div class="title dark-gray-text black grid-parent"><div class="grid-100">';
        $.each( jsonData, function( index, user){
            table += '<h2 class="title-2" id="qNumberv">'+'Question No.'+user.questID+'</h2>';
            table += '</div><div class="grid-100 grid-parent">';
            table += '<h1 class="grid-33 mobile-grid-50 " id="qCatv"><!--Question Category-->'+user.qCatName+'</h1>';
            table += '<h1 class="center-4text grid-33 mobile-grid-50-text4right " id="strikesv"><!--Strikes-->'+'00 Strikes'+'</h1>';
            table += '<h1 class="right right-title right-4text grid-33 mobile-grid-50-text4right " id="scorev"><!--Score-->'+'00 Scores'+'</h1</div></div><!--title END-->';
            table += '<section class="question-content"><!--Question Details--><h3 class="question shadow-body grid-100" id="qTitlev" ><!--Question Title-->'+user.questTitle+'</h3>';
            table += '<!--Question Description--><p class="question-detail shadow-body grid-100" id="qDescv" >'+user.questDesc+'</p></section></div><!--content END-->';    
        }); 
        $('div#qcontent').html(table);
    
    
    }
    

    其他信息:

    1. FIXED )警告错误显示parse errror syntax error Unexpected token < [object, object]
    2. prevenDefault(),关于$.each( jsonData, function( index, user){所在的行,但我不知道如何修复它。
    3. 查询本身很完美,但我不确定Perimeter的设备!
    4. 更新

      我发现我对控制器文件的调用状态如下:

      Request URL:http:.../app/controller/quiz_controller.php
      Request Method:POST
      Status Code:200 OK
      Request Headersview source
      Accept:application/json, text/javascript, */*; q=0.01
      Accept-Encoding:gzip,deflate,sdch
      Accept-Language:en-US,en;q=0.8
      Cache-Control:no-cache
      Connection:keep-alive
      Content-Length:26
      Content-Type:application/x-www-form-urlencoded; charset=UTF-8
      Cookie:PHPSESSID=fpm2m9kl4rd8ijgvqlkmv0kvc5
      Host:10.9.44.118
      Origin:http://10.9.44.118
      Pragma:no-cache
      Referer:http:.../app/equizz/public/index.php
      User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
      X-Requested-With:XMLHttpRequest
      Form Dataview sourceview URL encoded
      questID:3
      action:get_users
      Response Headersview source
      Connection:Keep-Alive
      Content-Length:292
      Content-Type:text/html
      Date:Mon, 25 Nov 2013 16:19:43 GMT
      Keep-Alive:timeout=5, max=100
      Server:Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
      X-Powered-By:PHP/5.4.7
      

      注意:出于安全原因,我用...替换了文件的实际路径。

1 个答案:

答案 0 :(得分:0)

将bindParam行更改为

$sth->bindParam(':parameter', $_POST['qid']);

修正了其余部分。

希望这可以帮助某些人遇到同样的麻烦:)

顺便说一句,我觉得它不够安全。 Plz修复或添加另一个答案,如果希望完善它,谢谢!