'Can't access fetch post data in controller: Codeigniter

I'm making a post request using fetch in my codeigniter project. The request looks like this

fetch('myurl/mycontroller', {
    method: 'POST',
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
         testdata: 123,
    })
 }).then((res) => {
    console.log(res);
 }).catch(console.log);

My controller looks like below

class MyController extends CI_Controller
{
    public function mycontroller()
    {
        $data = $this->input->post('testdata');
        return $data . " is the passed data.";
    }
}

But the data isn't getting passed to my controller. I echoed out $_POST and it gave me an empty array. Any idea what I'm doing wrong? I'm using codeigniter 2 (which I know is very old now)



Solution 1:[1]

So not completely sure about the actual reason but there might be some bug with the CI core of codeigniter which doesn't parse the data passed to controller using fetch. Using FormData() and axios I was able to resolve the issue.

 var postData = new FormData();
 postData.append('testdata', 123);
 axios.post('myurl/mycontroller', postData).then(function(response){
     console.log("success:", response);
 }).catch(function(error){
     console.log("error:", error);
 });

Solution 2:[2]

Use FormData() to submit it

var postData = new FormData();
postData.append('testdata', 123);

fetch('myurl/mycontroller', {
    method: 'POST',
    headers: {
        "Content-Type": "application/json"
    },
    body: postData
 }).then((res) => {
    console.log(res);
 }).catch(console.log);

Solution 3:[3]

This worked for me:

let postData = new FormData();
postData.append('testdata', 123);

fetch('myurl/mycontroller', {
    method: 'POST',
    mode: 'no-cors',
    headers: {
        "Content-Type": "application/json"
    },
    body: postData
 }).then((res) => {
    console.log(res);
 }).catch(console.log);

Use formData and mode: 'no-cors' in fetch settings.

Solution 4:[4]

I was having similar issue, i later fixed it without changing away from js fetch. Here is how i resolved it.

  • Make sure you have a Post route in your routing file to your CI controller
  • Make sure your fetch api url has no trailing '/'
let data = await fetch('https://example.com/api/login', {
              method: 'POST',
              credentials: 'same-origin',
              mode: 'same-origin',
              headers: {
                'Accept':       'application/json',
                'Content-Type': 'application/json',

              },
              body: JSON.stringify({email: '[email protected]', password: 'crypt887faked'}),
          });

          let response = await data.json();

This worked for me. I hope it works for someone also.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Justin S
Solution 2 Rully
Solution 3 f.llanquipichun
Solution 4 Sir'Energieman