Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post JSON to Codeigniter controller

I am trying to receive and parse a JSON object sent in a POST request using Codeigniter but I cannot "find" it.

This is my controller code:

public function parse () {

  $json = $this->input->post();
  $json = stripslashes($json);
  $json = json_decode($json);

  print_r($json);

}

This is my JSON object:

{"data":"value"}
like image 305
Jonathan Clark Avatar asked Dec 21 '11 20:12

Jonathan Clark


People also ask

What is getVar in CodeIgniter?

The getVar() method will pull from $_REQUEST, so will return any data from $_GET, $POST, or $_COOKIE. While this is convenient, you will often need to use a more specific method, like: $request->getGet() $request->getPost() $request->getServer()

How to Check request method in CodeIgniter 4?

Determining Request Type This can be checked with the isAJAX() and isCLI() methods: <? php // Check for AJAX request. if ($request->isAJAX()) { // ... } // Check for CLI Request if ($request->isCLI()) { // ... }


1 Answers

This is the correct way to do it.

$input_data = json_decode(trim(file_get_contents('php://input')), true);
like image 105
CMCDragonkai Avatar answered Oct 06 '22 11:10

CMCDragonkai