Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Why doesn't the message POST

When sending data, an empty value comes in Yii2, why is that? Data post: id, name.

JS

let is = document.querySelector("meta[name='csrf-token']").content,
    ss = document.querySelector("meta[name='csrf-param']").content;

 fetch("http://site.se/react/save-babysitter", {
  method: "POST",
  headers: {
   "Content-Type": "application/json",
   "Accept": "application/json",
   "csrf-param": ss,
   "X-CSRF-Token": is
  },
  body: JSON.stringify({
   'id': e.id,
   'name': this.state.ChangeName
  })
  }).then(response => response.json())
  .then((data) =>  console.log(data));

PHP

public function actionSaveBabysitter() {
 $request = Yii::$app->request;
 $post = $request->post('name');

 echo json_decode($post);

}

Code 200, post null

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

By default the Yii reads post params from $_POST global variable. But web server only parses request of body send as application/x-www-form-urlencoded or multipart/form-data. If you send data with Content-Type: application/json they are not parsed into $_POST variable.

To force Yii parsing JSON request you have to add json parser into yii\web\Request::$parsers property.

You can do that for example in your web.php config file:

'components' => [
    'request' => [
        'parsers' => [
            'application/json' => 'yii\web\JsonParser',
        ],
        // ... other configurations for request component
    ],
    // ... other components 
]
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!