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

161
Views
How to retrieve json object at root folder of api?

i try to make api request process on javascript with using root folder only, and there is some problem at input read when the client side request the post through the url. i also create index.php to automaticly execute the request, here is my javascipt code with root folder url

function save_user(){
    var usermail = document.getElementById("emailAddress").value;
    var username = document.getElementById("usernames").value;

    var xhr = new XMLHttpRequest();
    var url = "example.com/source/api/signup/access/";

    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-Type", "application/json");

    xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
      var json = JSON.parse(xhr.responseText);
    }
}

and if i directly request the url to the php file it work fine without any problem, like code below

function save_user(){
    var usermail = document.getElementById("emailAddress").value;
    var username = document.getElementById("usernames").value;

    var xhr = new XMLHttpRequest();
    var url = "example.com/source/api/signup/access/signup.php";

    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-Type", "application/json");

    xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
      var json = JSON.parse(xhr.responseText);
    }
}

<?php
header("Content-Type: application/json");
header("Access-Control-Allow-Headers: example.com");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: POST, OPTIONS");

require_once 'signup.php';

$obj = json_decode(file_get_contents("php://input"));
$cur_email = $obj->email;
$cur_name = $obj->username;

$container = new SighUp();
$respond = $container->SignupUser($cur_email, $cur_name);
echo json_encode($respond);
?>

is there any way to request post using root folder url only or it only can be done with direct url to the php file?

about 4 years ago · Juan Pablo Isaza
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!