开发者

problem returning a json encoded response after jQuery POST in codeigniter

开发者 https://www.devze.com 2023-02-19 03:43 出处:网络
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

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);

0

精彩评论

暂无评论...
验证码 换一张
取 消