Welcome Guest, Not a member yet? Register   Sign In
jquery ajax behaves strangely!!
#1

[eluser]Unknown[/eluser]
Hi,

I started working on codeigniter less than a week ago and i feel it's awesome.

Now i am stuck with an weird issue related to jquery $.post/$.ajax.

When i made a ajax call to the following function (which was inside a controller)
Code:
public function post_cweet()
    {
        if($this->input->post('entity_id'))
        {
            $this->load->helper("url");
            $entity_id = $this->input->post('entity_id');
            $entity_type_id = $this->input->post('e_t_id');
            $cweet_description = $this->input->post('desc');
            $cweet_image_url = $this->input->post('image_url');
            $posted_by = $this->input->post('posted_by');
            $posted_date =  unix_to_human(now());
            $tags = $this->input->post('tags');
            $event_id = $this->input->post('event_id');            
            $cweeter_employee_id = $this->input->server("REMOTE_USER");
            $this->load->model('cweets_model');
            $data = array(
                'entity_id'             => $entity_id,
                'entity_type_id'        => $entity_type_id,
                'cweet_description'     => $cweet_description,
                'cweet_image_url'       => $cweet_image_url,
                'posted_by'             => $posted_by,
                'posted_date'           => $posted_date,
                'tags'                  => $tags,
                'event_id'              => $event_id
            );            
            
            $cweet_id = $this->cweets_model->add_cweets($data);
  
            $time_diff = timespan(human_to_unix($posted_date),time());
            $return_data = array(
                "cweet_id"  => $cweet_id,
                'entity_id'             => $entity_id,
                'entity_type_id'        => $entity_type_id,
                'cweet_description'     => $cweet_description,
                'cweet_image_url'       => $cweet_image_url,
                'posted_by'             => $posted_by,
                'posted_date'           => $posted_date,
                'tags'                  => $tags,
                'event_id'              => $event_id,
                'time_diff'             => $time_diff
            );
            
$cweet_description = '#'.$tags.' '.$cweet_description;
echo json_encode($return_data);
            //exit;
        }
        
    }

the ajax calls failed with error status code as 0.But i could see that the data were inserted into the database. The problem was i am not getting the json encoded data which was being returned by the server.

here's my ajax call
Code:
$.post(url, { entity_id: entity_id, e_t_id: etype_id, desc:desc, posted_by: posted_by, event_id:event_id,image_url:main_img_path,tags:tags},
function(data) {
alert("success");
});

When i debugged using firebug, the error was at the following line in jquery-1.8.2.js
Code:
xhr.send( ( s.hasContent && s.data ) || null );

And one more thing, the server did return the json encoded data when i added exit after the echo statement.

I don't know the cause. How can i make it work without adding exit statement? Can anyone help me on this?

#2

[eluser]Unknown[/eluser]
I got it right! It works fine when i send the respond using the following code

Code:
$this->output->set_output(json_encode($return_data));

instead of

Code:
echo json_encode($return_data);

but again, i have two servers, echoing the response data back works fine in one server.. but in another server it works only if i use $this->output->set_output().

But why isn't my second server simply echo back my response data? Is there any problem related to output buffering?




Theme © iAndrew 2016 - Forum software by © MyBB