Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get whole post body within codeigniter controller

I'm running XMLHttpRequest request like this:

var data = JSON.stringify({
    name : "123",
    id : 12
});

window.console.log("Submitting: " + data);
var req = new XMLHttpRequest();
req.open('POST', "http://localhost/index.php/lorem/ipsum", true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.onreadystatechange = function() {
    if (  req.readyState==4) {
        window.console.log( "Sent back: " + req.responseText );
    }
}
req.send(data);

as you can see there's no name for parameter being passed.

now I want to read that JSON data inside ipsum function of lorem controller. How can I do this? $this->input->post(); returns false :(

like image 527
David Avatar asked Dec 28 '22 04:12

David


2 Answers

Use file_get_contents('php://input')

like image 196
Morgan Cheng Avatar answered Jan 09 '23 17:01

Morgan Cheng


even though your turning a JSON Object into string your not assigning a key to the string, what so server side does not have an identifier for your string.

What you should do is:

req.send("json=" + data);

then in PHP use:

$this->input->post("json");

To receive data without the need of KV Pairs you can use stdin i suppose.

http://php.net/manual/en/wrappers.php.php

or even using a variable designed for this purpose:

$HTTP_RAW_POST_DATA

like image 31
RobertPitt Avatar answered Jan 09 '23 17:01

RobertPitt