我想从 mysql 表(仅一个字段)取回一些数据,以便使用该数据自动填充表单。
我的 ajax() 有问题,它返回 ok (200),但它仍然运行我添加到其中的错误函数。
$('#group_id').on('click', function() {
$.ajax({
url: "localhost/zamzamtravel/home/getPrevApplicantData",
dataType: 'json',
type: 'GET',
data: {group_id:1},
contentType: "application/json",
success: function(response,data) { alert(response); alert(data); },
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr);
alert(xhr.status);
alert(thrownError);
}
});
我从我的 php 脚本(我使用 codeigniter)获取 json 对象,就像我的 Controller 一样:
public function getPrevApplicantData()
{
$this->output->set_content_type('application/json');
$data = $this->travel_model->getPrevApplicantData();
echo json_encode($data);
}
这是在模型中检索并返回到上述 Controller 函数的数据:
public function getPrevApplicantData()
{
$group_id = $this->input->get('group_id');
$this->db->select('general_info.address_telephone');
$this->db->from('general_info');
$this->db->join('groups', 'groups.group_id = general_info.group_id');
$this->db->where('general_info.group_id', 1);
$this->db->distinct();
$this->db->limit(1);
$this->db->order_by('general_info.customer_id','asc');
$res = $this->db->get();
return $res->result_array();
}
我附加到单击事件的输入类型是单选按钮:
<input type="radio" name="group_id" id="group_id">
(我想也许我不需要表单,只需制作一个普通按钮,当单击该按钮时它可以运行ajax功能?)
我在 jQuery 的 devtools 中也遇到错误:xhr.send( ( s.hasContent && s.data ) || null );
存储查询后从模型函数返回的数组的结构如下:
Array
(
[0] => Array
(
[address_telephone] => 1 somehouse avenue-highpoint-nc-27262-3365550493
)
)
null
我真的很想知道出了什么问题,我想做的只是简单的获取和显示:P
最佳答案
我认为 php 的 Json 输出需要将状态项设置为 200,然后对数据使用响应对象。
{
status : 200,
response:{
//data here
}
)
关于JQuery ajax() 返回 200 但未运行成功函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20729180/