Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
AJAX sending data in GET request is not working

I have an AJAX function running from my frontend looking like this:

//api.js

function getTransactions(authToken) {
  $.ajax({
    type: 'GET',
    data: {
      'authToken':
        'ACE8166BCD0F5B8FB5FF693F3B4056160D957A0E57FC9912EE70E9231D03166B9661BCB96BD678F3BD65CFD6DA651F1E5B5D28E294CA88C0EEE2B3C7093699EAA731409A1F8DA5C772E2335EC67ED410E20ED9FBB35B6918804879BE3D3FE1329F2348F1FB83B9BC4992F7EA75D1ED4A4F9A684371CB0D0CF09E131ADAB905CAE4454C4C9D81A9A48B05AA77B4D6F7D7C5A596C021DA368F6E356A5750EB2D117C8E02782731F7641526ACBA061EEDDB2339EABB3B179F39738D8AEF4510E612A2D11BCC74D67C3C4D989A86BB9EE710342042D2A71A364B0D17D799E28D51458AD44F53F3930E31387FF52BAD141D4AA467384282226AB2B1B55A5B45CCAF5F61DDAD770E3BA9210BBE7B926BD594F52F17F053B6C0366B4CDC4F2F147648A545ABDDD854FF778F4EA0A2672E0D249B',
    },
    crossDomain: true,
    dataType: 'JSON',
    url: 'http://localhost:8000/transactions',
  }).done(function (data) {
.....

I have been trying to pass the authToken parameter to the data object, and then retrieve it in the PHP backend using (case transactions):

//index.php

$request = $_SERVER["REQUEST_URI"];

switch ($request) {
  case "/login" :
    require __DIR__ . "/login.php";
    $userEmail = $_POST["userEmail"];
    $userPassword = $_POST["userPassword"];
    echo login($userEmail, $userPassword);
    break;
  case "/transactions" :
    require __DIR__ . "/transactions.php";
    $authToken = $_GET["authToken"];
    getTransactions($authToken);
    break;
}

If I hardcode the authToken into the getTransactions function inside case "/transactions", it works fine.

(EDIT: even if I hardcode the token in index.php, if I also hardcode it into the AJAX function (which shouldn't matter), the whole process doesn't return any data).

But there's something, either in the $_GET() in index.php, or in the AJAX function that it preventing the data from getting into the getTransactions function in index.php.

I have tried putting strings around the authToken key in the data object, also using the authToken parameter, as well as JSON.stringify-ing the data object. Further, I have changed/removed the dataType, and exhausted every other method suggested by stack overflow and other websites.

This is especially difficult because it's my first time using PHP, and I don't know how to show echos in my terminal, so I can't see any output from the PHP files.

Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

as @CBroe said, the authToken query parameter was messing with the routing for /transactions. What I did was add this code to my $request variable:

$request = strtok($_SERVER["REQUEST_URI"], "?");

the strtok() function allowed me to strip off the end of the URI before the ?, and the /transactions case was read properly.

Thanks CBroe!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda