1
votes

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
show the res code....Bhojendra Rauniyar
The error message suggests that your server is returning something other than just your JSON. < could be the first character of <!doctype... for example. You need to find out what the server is actually returning. What do you see if you access that page with a browser?user1864610
Don't bother accessing "that page with a browser", just use your browser's Network console to inspect the AJAX request and responsePhil
What is res before you attempt to parse it?Michael Angstadt
paste your res to a JSON linter like e.g. jsonlint.com . It will tell you exactly where the problems are.Anticom

2 Answers

0
votes

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').

0
votes

All the htmls were xdebug messages listing in a table. The error was produced because the user wasn't logged in while searching. By setting $loginUser = new StdClass; $loginUser->id = ''; the problem resolves.