Jump to content

input get / post inside of process module


pwFoo
 Share

Recommended Posts

I try to get POST data inside of my process module. Post request is sent by ajax with payload.

image.png.5fe881577cc2edd4ceb2e4e2c797023c.png

 

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 weeks later...

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();

 

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...