Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda