Welcome Guest, Not a member yet? Register   Sign In
Ajax posted data is not visible in controller method
#1

[eluser]Blue Sapphire[/eluser]
Hi!
Following is my ajax code:
Code:
jQuery.ajax({
    'type': 'POST',
    'dataType': 'json',                    
    'contentType': 'application/json; charset=utf-8',
    'url': 'http://localhost/main/user/view',                      
    'data': ({'active' : 'active_value'}),
    'success': function(data){
    alert('Good Return Text');
    }                
});

My CI controller's method code is as follows:
Code:
function view()
{    
    if($this->input->is_ajax_request()){
    var_dump($this->input->post('active'));  
       }
}

The Firebug is showing following post resuest :
Quote:active=active_value

The Firebug is showing following response :
Quote:bool(false)

Can someone guide me what Iam doing wrong.

NOTE: CI version: 2.0 , jQuery version: 1.5

Thanks in advance
#2

[eluser]CroNiX[/eluser]
You are telling jquery to expect a json response, but you are not sending json from your controller. Also, I think you should probably leave the contentType the default.
#3

[eluser]KarlBallard[/eluser]
I admit I have zero experience with JSON but shouldn't your method be more like:
Code:
function view()
{    
    if($this->input->is_ajax_request())
    {
        echo json_encode($this->input->post('active'));  
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB