This is very disheartening ! I tried to send a simple ajax post to my local web app and the param is empty . To debug it , I opened fiddler yet the same result is happening!
I have this Controller
class Site extends CI_Controller {
public function __construct() {
parent::__construct();
} //etc
With this function
public function hello() {
var_dump($_SERVER['REQUEST_METHOD']);
var_dump($_SERVER['REQUEST_URI']);
$hello = $this->input->post("hello");
echo "hello is $hello";
}
Now I try to test this in Fiddler , I go to Composer,choose a new Post request and put in the body hello=wtsgppp.
Here is the result of hello function in fiddler:
string(4) "POST"
string(25) "/cmd/index.php/site/hello"
hello is
As you can see hello is empty . Any help with be much appreciated!!
To make matters even more annoying - Once I change the http request in fiddler to GET and add hello in the url string (and change the controller to echo get instead of post) it works! So why does get work but post doesn't ?
PHP won't populate the $_POST
array unless it sees a known content type in the header.
In your Fiddle request, try adding the following HTTP header:
Content-Type: application/x-www-form-urlencoded
This is because the way that PHP decodes the raw POST body and injects it into the $_POST
variable (which is what CI looks at) is dependent on the way the request is sent to the server.
Edit: If you aren't seeing it in your XHR/AJAX post either, be sure you're setting the Content-Type
header as well. jQuery (and most JS libraries) will do this automatically for you, but you have to do it on your own if you don't use a library:
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");