'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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source