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

163
Views
How to get the json data from xhr fetch (after CORS) back in php

I am fetching JSON data with CORS and I am getting the JSON data in my console successfully. Now I want to display the fetched data with PHP. What are the next steps after the code below to get the fetched data into PHP?

    <script>
        fetch("http://localhost:8000/get_users.php", {
            method: 'post',
            credentials: 'include'
        }).then(response => {
            return response.json();
        }).then(users => {
            console.log(users);
        });
    </script>

I tried to get it in $_POST:

    $posted_data = array();
    if (!empty($_POST['users'])) {
        $posted_data = json_decode($_POST['users'], true);
    }
    print_r($posted_data);

But users is undefined.

Does someone know how to get the fetched JSON users into PHP? Thanks!

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

0

Okay I found a solution that might bring me one good step further. I need to JSON.stringify() my fetched JSON, that was the issue.

UPDATED CODE:

<script>
        fetch("http://localhost:8000/get_users.php", {
            method: 'post',
            credentials: 'include'
        }).then(response => {
            return response.json();
        }).then(users => {
            console.log(users); 
            var users = JSON.stringify(users);
            alert(users);
        });
    </script>

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!