Jump to content

get post value from javascript fetch


997 998 999
 Share

Recommended Posts

Hello,

I'm trying to edit the field of a page with a value from a javascript fetch post request but I can't get the

file_get_contents("php://input")

or

$_POST

value in my PHP file.

I created a `update` template and a `update` page (to allow access from fetch request and avoid 404/403/... errors) containing this code :

$data = file_get_contents('php://input');
echo $data;

# test edit the page field with random value
#$home = $pages->get("/");
#$home->of(false);
#$home->testfield = "test value";
#$home->save();

# test fetch response  with random value
#echo "test fetch response";

That I fetch request with my client javascript like this :

    fetch('/update', {
        method: 'POST',
        body: 'test'
    })
    .then(function(response) {
        return response.text();
    })
    .then(function(data) {
        console.log(data);
    })
    .catch(error => console.log('error'))
}

The field edit works fine and I get the test response from the `update` file but I can't get the posted value, I only get `<empty string>`. It is working fine outside of ProcessWire so I guess this is related to how PW handle $_POST ?

I found this post where it seems to be working :

 

Is there any way to get post value from javascript fetch request? What am I doing wrong?

 

Thank you

 

 

 

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

×
×
  • Create New...