Mysterious Error AJAX SyntaxError: Unexpected token < in JSON at position 0
Mysterious Error AJAX SyntaxError: Unexpected token < in JSON at position 0
当我运行 AJAX 自动完成代码时出现解析错误。响应看起来不错。但是我不明白为什么会发生错误。
这是脚本和 HTML 代码
<script type="text/javascript"><!--
$('input[name=\'filter_parent\']').autocomplete({
'source': function(request, response) {
$.ajax({
url: 'index.php?route=catalog/category/autocompletecat&user_token={{ user_token }}&filter_parent=' + encodeURIComponent(request),
dataType: 'json',
success: function(json) {
response($.map(json, function(item) {
return {
label: item['name'],
value: item['category_id']
}
}));
},
beforeSend: function(jqXHR) {},
error: function(jqXHR, textStatus, errorThrown) {console.log(errorThrown);},
complete: function(jqXHR,status) {}
});
},
'select': function(item) {
$('input[name=\'filter_parent\']').val('');
}
});
//--></script>
<div id="filter-category" class="col-md-3 col-md-push-9 col-sm-12 hidden-sm hidden-xs">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-filter"></i> {{ text_filter }}</h3>
</div>
<div class="panel-body">
<div class="form-group">
<label class="control-label" for="input-parent">{{ entry_parent }}</label>
<input type="text" name="filter_parent" value="{{ filter_parent }}" placeholder="{{ entry_parent }}" id="input-parent" class="form-control" />
</div>
<div class="form-group">
<label class="control-label" for="input-status">{{ entry_status }}</label>
<select name="filter_status" id="input-status" class="form-control">
<option value=""></option>
{% if filter_status == '1' %}
<option value="1" selected="selected">{{ text_enabled }}</option>
{% else %}
<option value="1">{{ text_enabled }}</option>
{% endif %}
{% if filter_status == '0' %}
<option value="0" selected="selected">{{ text_disabled }}</option>
{% else %}
<option value="0">{{ text_disabled }}</option>
{% endif %}
</select>
</div>
<div class="form-group text-right">
<button type="button" id="button-filter" class="btn btn-default"><i class="fa fa-filter"></i> {{ button_filter }}</button>
</div>
</div>
</div>
</div>
这是具有 json 响应的控制器
public function autocompletecat() {
$json = array();
$json[] = array(
'category_id' => 0,
'name' => strip_tags(html_entity_decode('---All---', ENT_QUOTES, 'UTF-8'))
);
if (isset($this->request->get['filter_parent'])) {
$this->load->model('catalog/category');
$filter_data = array(
'filter_parent' => $this->request->get['filter_parent'],
'sort' => 'name',
'order' => 'ASC',
'start' => 0,
'limit' => 5
);
$results = $this->model_catalog_category->getSuperParentCategories($filter_data);
foreach ($results as $result) {
$json[] = array(
'category_id' => $result['category_id'],
'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
);
}
}
$sort_order = array();
foreach ($json as $key => $value) {
$sort_order[$key] = $value['name'];
}
array_multisort($sort_order, SORT_ASC, $json);
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
[{"category_id":0,"name":"---All---"}]
我尝试了其他功能,none 可以正常使用。这太神秘了,我不知道是什么原因导致这个错误。请帮我解决这个问题。我几乎为此花了一整天。
提前致谢
终于找到了。由于 user_token 未在控制器中声明而导致错误。
$data['user_token'] = $this->session->data['user_token'];
当我运行 AJAX 自动完成代码时出现解析错误。响应看起来不错。但是我不明白为什么会发生错误。
这是脚本和 HTML 代码
<script type="text/javascript"><!--
$('input[name=\'filter_parent\']').autocomplete({
'source': function(request, response) {
$.ajax({
url: 'index.php?route=catalog/category/autocompletecat&user_token={{ user_token }}&filter_parent=' + encodeURIComponent(request),
dataType: 'json',
success: function(json) {
response($.map(json, function(item) {
return {
label: item['name'],
value: item['category_id']
}
}));
},
beforeSend: function(jqXHR) {},
error: function(jqXHR, textStatus, errorThrown) {console.log(errorThrown);},
complete: function(jqXHR,status) {}
});
},
'select': function(item) {
$('input[name=\'filter_parent\']').val('');
}
});
//--></script>
<div id="filter-category" class="col-md-3 col-md-push-9 col-sm-12 hidden-sm hidden-xs">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-filter"></i> {{ text_filter }}</h3>
</div>
<div class="panel-body">
<div class="form-group">
<label class="control-label" for="input-parent">{{ entry_parent }}</label>
<input type="text" name="filter_parent" value="{{ filter_parent }}" placeholder="{{ entry_parent }}" id="input-parent" class="form-control" />
</div>
<div class="form-group">
<label class="control-label" for="input-status">{{ entry_status }}</label>
<select name="filter_status" id="input-status" class="form-control">
<option value=""></option>
{% if filter_status == '1' %}
<option value="1" selected="selected">{{ text_enabled }}</option>
{% else %}
<option value="1">{{ text_enabled }}</option>
{% endif %}
{% if filter_status == '0' %}
<option value="0" selected="selected">{{ text_disabled }}</option>
{% else %}
<option value="0">{{ text_disabled }}</option>
{% endif %}
</select>
</div>
<div class="form-group text-right">
<button type="button" id="button-filter" class="btn btn-default"><i class="fa fa-filter"></i> {{ button_filter }}</button>
</div>
</div>
</div>
</div>
这是具有 json 响应的控制器
public function autocompletecat() {
$json = array();
$json[] = array(
'category_id' => 0,
'name' => strip_tags(html_entity_decode('---All---', ENT_QUOTES, 'UTF-8'))
);
if (isset($this->request->get['filter_parent'])) {
$this->load->model('catalog/category');
$filter_data = array(
'filter_parent' => $this->request->get['filter_parent'],
'sort' => 'name',
'order' => 'ASC',
'start' => 0,
'limit' => 5
);
$results = $this->model_catalog_category->getSuperParentCategories($filter_data);
foreach ($results as $result) {
$json[] = array(
'category_id' => $result['category_id'],
'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
);
}
}
$sort_order = array();
foreach ($json as $key => $value) {
$sort_order[$key] = $value['name'];
}
array_multisort($sort_order, SORT_ASC, $json);
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
[{"category_id":0,"name":"---All---"}]
我尝试了其他功能,none 可以正常使用。这太神秘了,我不知道是什么原因导致这个错误。请帮我解决这个问题。我几乎为此花了一整天。
提前致谢
终于找到了。由于 user_token 未在控制器中声明而导致错误。
$data['user_token'] = $this->session->data['user_token'];