【发布时间】:2013-12-22 12:02:23
【问题描述】:
我想从 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 对象,就像我的控制器一样:
public function getPrevApplicantData()
{
$this->output->set_content_type('application/json');
$data = $this->travel_model->getPrevApplicantData();
echo json_encode($data);
}
这是在模型中检索并返回给上述控制器函数的数据:
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
【问题讨论】:
-
在您请求本地主机的代码中,您是否在生产环境中请求不同的域?
-
@ToniMichelCaubet 不,它在我的应用程序中
-
试试:
success: function(response,data) { alert(response); alert(data); } -
您在执行
alert('ok' + data);时可能会遇到类型错误,因为在这种情况下data是一个对象而不是字符串。 -
这是一个关于 CI 和创建休息服务的好教程。 REST 很棘手,但值得努力。 net.tutsplus.com/tutorials/php/…
标签: jquery ajax codeigniter