在 codeigniter 中的 jQuery POST 之后返回 json 编码响应的问题

发布于 2024-10-25 20:25:08 字数 1037 浏览 0 评论 0原文

我在使用 jQuery post 后收到响应时遇到问题。我正在创建的脚本正确发布数据并正确插入到数据库中,但是当我尝试返回 json 编码响应时,没有收到数据。

这是我正在使用的代码:

jQuery.ajax({
     success: function(data) {
            if (data)
            {
            alert("DATA RECEIVED");
            }
            },
          data: {action: 'create', section: JSON.stringify(values)},
          type: 'POST',
          dataType: 'json',
            url: "fields/sections/create" 
        });

在我的控制器中

$section = json_decode($this->input->post('section'));


            $this->load->model('mdl_fields');


            $section_id = $this->mdl_fields->create_section($section->name, $section->row);

            if($section_id) {

                $data=array(
"section" => $section_id,
"confirm" => 'Section Has Been Created Successfully'
);
return json_encode($data);
}

,我已经检查了是否有 $section_id ,并且我已经打印了 json_encode($data) 以检查它是否正确,确实如此,但我仍然没有收到响应。

在过去的几个小时里,我一直在努力解决这个问题,所以任何帮助将不胜感激。

谢谢

I am having problems receiving response after using jQuery post. The script I am creating posts the data correctly and inserts correctly to the database however when I try and return a json encoded response no data is received.

Here is the code I am using:

jQuery.ajax({
     success: function(data) {
            if (data)
            {
            alert("DATA RECEIVED");
            }
            },
          data: {action: 'create', section: JSON.stringify(values)},
          type: 'POST',
          dataType: 'json',
            url: "fields/sections/create" 
        });

In my controller I have

$section = json_decode($this->input->post('section'));


            $this->load->model('mdl_fields');


            $section_id = $this->mdl_fields->create_section($section->name, $section->row);

            if($section_id) {

                $data=array(
"section" => $section_id,
"confirm" => 'Section Has Been Created Successfully'
);
return json_encode($data);
}

I have checked there is a $section_id and I have printed json_encode($data) to check it is correct, which it is, but I am still not receiving a response.

I have been tearing my hair out trying to solve this for the last few hours so any help would be appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

固执像三岁 2024-11-01 20:25:08

也许您只需要制作类似的东西

echo json_encode($data);

,即可生成 JSON 兼容格式的 $data 结构的纯输出。看看 json_encode 它返回字符串并且不产生任何输出!

Maybe you simply need to make something like

echo json_encode($data);

that would generate plain output of $data structure in JSON compatible format. Take a look at json_encode it return string and does NOT produce any output!

纸短情长 2024-11-01 20:25:08

尝试:echo json_encode($data);

Try : echo json_encode($data);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文