【发布时间】:2018-12-05 09:55:57
【问题描述】:
我在使用 JSON 提交数据时遇到 jQuery 显示响应的问题。
这是我的 HTML
<input required="" width="100%" name="category_id" id="category_id"
class="form-control">
<input required="" width="100%" name="agent_id" id="agent_id"
class="form-control">
<input required="" width="100%" name="year" id="year"
class="form-control">
<button type="button" onclick="historyForm()" title="fetch history">
view</button>
<div id="ajax-content">
</div>
这是我的脚本
<script type="text/javascript">
function historyForm() {
$.ajax({
url: '<?php echo base_url();?>index.php?staff/fetch_history',
method: 'POST',
dataType: 'json',
data: {
category_id: $("#category_id").val(),
agent_id: $("#agent_id").val(),
year: $("#year").val(),
},
success: function (response) {
jQuery('#ajax-content').html(response);
}
});
}
</script>
这是我的控制器
function fetch_history()
{
$response = array();
$category_id = $_POST["category_id"];
$agent_id = $_POST["agent_id"];
$year = $_POST["year"];
$page_data['category_id'] = $category_id;
$page_data['agent_id'] = $agent_id;
$page_data['year'] = $year;
$response = $this->load->view('backend/staff/history_result' ,
$page_data);
echo json_encode($response);
}
我没有看到任何响应和任何错误,但是当我尝试手动编写 $page_data 时说 $year = '2018', $agent_id = 1 and $category_id = 2 我得到了响应。
可能是什么问题?
【问题讨论】:
-
如果用 javascript 编写会得到什么:
console.log(response)insuccess方法? -
另外
url在ajax 中也可以吗? -
@Snake,感谢您的评论。
console.log(response)也不显示任何内容。 -
来自 ajax 的 url 正确吗?
$_POST[...]给你正确的信息? -
@Snakes,当我假设
$year = '2018'、$category_id = 1和$agent_id = 2我得到了很好的回应。但是当我使用 JSON 提交结果时,什么都没有返回。
标签: jquery json ajax codeigniter