我正在构建一个搜索列表,会列出一些用户,当我尝试加载更多页面以显示更多用户时,我收到此错误:
SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符
var result = JSON.parse(res);
这是我的index.volt中的ajax函数
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, []);
}
});
这是我的Controller中的searchAction():
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)返回:
<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}
SearchController.php中的第62行引用searchAction()中的$list = $searchUserModel->findTeachers($q, $loginUser->id, ($loginUser?true:false), $page, $limit, $sort);
所以我的问题是如何在res中只返回那些没有那些html的JSON?
答案 0 :(得分:0)
要发送JSON响应,您必须禁用该视图。
$this->view->disable();
我还会使用$this->response->setContentType('application/json')
明确设置内容类型。
答案 1 :(得分:0)
所有htmls都是在表格中列出的xdebug消息。产生错误是因为用户在搜索时未登录。通过设置$loginUser = new StdClass; $loginUser->id = '';
,问题就会解决。