weixin_33701251 2015-07-02 03:39 采纳率: 0%
浏览 105

如何只在res中获取JSON?

I am building a search which will list a number of users and I am getting this error when I am trying to load more pages to show more users:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

var result = JSON.parse(res);

Here is my ajax function in my index.volt

function(cb){
        $.ajax({
            url: '/search/search?q=' + mapObject.q + '&sort=<?php echo $sort;?>' +  '&page=' + mapObject.page,
            data:{},
            success: function(res) {
                var result = JSON.parse(res);
                if (!result.status){
                    return cb(null, result.list);
                }else{
                    return cb(null, []);
                }
            },
            error: function(xhr, ajaxOptions, thrownError) {
                cb(null, []);
             }
    });

And here is my searchAction() in my Controller:

public function searchAction()
    {
        $q = $this->request->get("q");
        $sort = $this->request->get("sort");
        $searchUserModel = new SearchUsers();
        $loginUser = $this->component->user->getSessionUser();
        $page = $this->request->get("page");
        $limit = 1;
        if (!$page){
            $page = 1;
        }
        $list = $searchUserModel->findTeachers($q, $loginUser->id, ($loginUser?true:false), $page, $limit, $sort);
        $list['status'] = true;
        echo json_encode($list);
    }

Console.log(res) returned:

<br /> <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'> <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Trying to get property of non-object in C:\xampp\htdocs\tute\app\controllers\SearchController.php on line <i>62</i></th></tr> <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr> <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr> <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0009</td><td bgcolor='#eeeeec' align='right'>192016</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\xampp\htdocs\tute\public\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr> <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0019</td><td bgcolor='#eeeeec' align='right'>205824</td><td bgcolor='#eeeeec'><a href='http://www.php.net/Phalcon\Mvc\Application.handle' target='_new'>handle</a> ( )</td><td title='C:\xampp\htdocs\tute\public\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>239</td></tr> <tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0032</td><td bgcolor='#eeeeec' align='right'>256968</td><td bgcolor='#eeeeec'><a href='http://www.php.net/Phalcon\Dispatcher.dispatch' target='_new'>dispatch</a> ( )</td><td title='C:\xampp\htdocs\tute\public\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>239</td></tr> <tr><td bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec' align='center'>0.0161</td><td bgcolor='#eeeeec' align='right'>452064</td><td bgcolor='#eeeeec'>SearchController->searchAction( )</td><td title='C:\xampp\htdocs\tute\public\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>239</td></tr> </table></font> {"total":"2","numPages":2,"page":"2","list":[{"id":"23","lesson_complete":"4","num_rating":"4","rating":"3.75","intro_video":"0","uploads":"0","experience_filled":"1","lat":"9999","lng":"9999","usr_id":"23","price":"30","distance":"0","rank":"4.33","search_id":"18","firstname":"Yolo","lastname":"yolo","avatar":null,"lan":"lang_zh-CN","usr_teach":1,"usr_rate":"30","skills":"Ice Hockey"}],"status":true}

Line 62 in SearchController.php refers to $list = $searchUserModel->findTeachers($q, $loginUser->id, ($loginUser?true:false), $page, $limit, $sort);in the searchAction()

So my question is how do I only return the JSON without that bunch of html in the res?

  • 写回答

2条回答 默认 最新

  • weixin_33698823 2015-07-02 08:18
    关注

    To send a JSON response you have to disable the view.

    $this->view->disable();
    

    I would also explicitly set the content type with $this->response->setContentType('application/json').

    评论

报告相同问题?

悬赏问题

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