pwFoo Posted June 24, 2018 Posted June 24, 2018 I try to get POST data inside of my process module. Post request is sent by ajax with payload. But $this->input->post() or $_POST is empty here? public function executeSubscribe() { // get subscription from client and save to user / db //$array = array("me" => 'you'); // works fine! Can output with console.log at client side //$array = $this->input->post('subscription'); // empty //$array = $this->input->post(); // empty //$array = (array) $this->input->post(); // empty //$array = $_POST; // empty $array = (array) $_POST; // empty $this->render($array); } protected function render($out) { header('Content-Type: application/json'); echo json_encode($out); exit(); } I tried the post url with and without slash (I know the topics about ajax, post and trailing slash), but POST data is empty every time. Is it process module related? GET works fine in "execute()" method.
pwFoo Posted June 24, 2018 Author Posted June 24, 2018 It's fetch() related... fetch(subscribeUrl, { method: 'post', credentials: 'same-origin', headers: { 'Content-type': 'application/json' }, body: JSON.stringify({ subscription: subscription }) }) Change content type... fetch(subscribeUrl, { method: 'post', credentials: 'same-origin', //headers: { 'Content-type': 'application/json' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded'}, body: JSON.stringify({ subscription: subscription }) }) https://stackoverflow.com/questions/36669911/post-not-retrieving-data-from-javascripts-fetch
pwFoo Posted June 24, 2018 Author Posted June 24, 2018 Searched for a PW / PHP issue and missed the needed Content-Type to get $_POST populated ?
pwFoo Posted July 7, 2018 Author Posted July 7, 2018 Works fine with FormData object and without set Content-Type Header... var body = new FormData(); body.append("name", "Bob"); body.append("age", 27); fetch("http://127.0.0.1/fetch.php", { method: "post", body: body }).then(function(response) { console.log(response); return response.JSON(); }).then(function(result) { console.log(result); }); <?php $res = $_POST["name"]; print_r($res); die(); 1 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now