php - How do I get only JSON in res? -
i building search list number of users , getting error when trying load more pages show more users:
syntaxerror: json.parse: unexpected character @ line 1 column 1 of json data
var result = json.parse(res);
here ajax function in 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 searchaction() in 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 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 $list = $searchusermodel->findteachers($q, $loginuser->id, ($loginuser?true:false), $page, $limit, $sort);
in searchaction()
so question how return json without bunch of html in res?
to send json response have disable view.
$this->view->disable();
i explicitly set content type $this->response->setcontenttype('application/json')
.
Comments
Post a Comment