0

On the client side I have :

var postData = {
    "id" : id,
    "message" : message
};

console.log(postData);

$.ajax({
    type: "POST",
    url: "controller/function",
    data: postData, 
    success: function(){
        alert(id + ' ' + message);
    }
});

This appears to be working properly as I can see the correct post parameters in chrome dev tools. In my codeigniter controller I have tried:

echo 'postid' . $_POST['id'].' '.$_POST['message'];

$postData=$this->input->post('id');

var_dump($postData); exit;

I'm getting:

Message: Undefined index: id
Message: Undefined index: message

boolean(false)

the $_POST array is empty.

How can I fix this? Thank you for your help

4 Answers 4

1

you may add dataType:'json' in your ajax options

$.ajax({
       type: "POST",
       url: "controller/function",
       data: postData,
       dataType:'json',
       success: function(){
           alert(id + ' ' + message);
       }
});
Sign up to request clarification or add additional context in comments.

7 Comments

try to add contentType attribute as well.
Hi guys, I tried the answer but it doesn't work. I'd like to add a contentType , but what should it be set to. Ali, what should I json_decode?
what ci version do you use?
I m using jquery 1.7.2
it does work here....<html> <head> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> var id = 123, message = 'test', postData = { "id" : id, "message" : message }; console.log(postData); $.ajax({ type: "POST", url: "test/do_action", data: postData, success: function(){ alert(id + ' ' + message); } }); </script> </head> <body> </body> </html>
|
0

Did you configure your CI to use CSRF protection? If yes, you will need to include the CSRF field and value to your POST request.

Comments

0

Add dataType:'json' parameter for the $.ajax at client side.

Comments

0
$.ajax({
    type: "POST",
    url: "controller/function",
    data: postData, 
    success: function(data){
        alert(data.id + ' ' + data.message);
    }
});

ajax return json object in data , you forgot to written data in function

1 Comment

Please add some explanation to the proposed solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.