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
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!
Try : echo json_encode($data);
精彩评论