JQuery ajax() 返回 200 但未运行成功函数

标签 jquery ajax codeigniter

我想从 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/

相关文章:

php - codeigniter 查询 : how to take all the record from a column in a table when i use sum Select function to get data from another table

php - 需要用户体验数据库和用户级别的帮助

php - 带有修改表的 Codeigniter 分页

javascript - 下拉菜单显示,但当我单击它时没有任何反应,有人可以告诉我为什么吗?

javascript - 使用jquery对页面上的div元素进行排序

Python 3.6.2/Selenium 2.0/AJAX - 如何等待脚本结束服务器请求

javascript - Jquery Ajax API错误: 400 Bad Request

javascript - 如何使用 mysql 将额外的过滤选项添加到 Ajax PHP 搜索

javascript - jQuery的谷歌TTS没有声音

javascript - 如何在文档标题上添加 (1) 文本通知