在 codeigniter 中的 jQuery POST 之后返回 json 编码响应的问题
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您只需要制作类似的东西
,即可生成 JSON 兼容格式的 $data 结构的纯输出。看看 json_encode 它返回字符串并且不产生任何输出!
Maybe you simply need to make something like
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!
尝试:
echo json_encode($data);
Try :
echo json_encode($data);