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

205
Views
Los valores de matriz no pueden tener éxito en Ajax con JSON.parse

Tengo los siguientes códigos jQuery y Ajax

 $("#grabIdCodeDetails").click(function() { var idCodeForDetails = $("#idCode").val(); if (idCodeForDetails.trim() == "") { errorFunction("Please key in an ID Code"); hidePreviousShownResult(); } else { $.ajax({ type: "POST", url: "fetchIdCodeDetailsFromDatabaseAjax.php", data: { idCodeForDetails: idCodeForDetails }, success: function(fetchedDetails) { var returnedData = JSON.parse(fetchedDetails); alert(returnedData['needle']); } }); } });

Los siguientes son los códigos en la página fetchIdCodeDetailsFromDatabaseAjax.php

 $resultArray = []; array_push($resultArray, $DeviceName, $Start_Date, $Customer, $TotalPinCount, $NeedleType, $DeliveryDate); $DeviceName = $resultArray[0]; $Start_Date = $resultArray[1]; $Customer = $resultArray[2]; $TotalPinCount = $resultArray[3]; $NeedleType = $resultArray[4]; $DeliveryDate = $resultArray[5]; echo json_encode(array( 'needle' => $NeedleType, 'deviceName' => $DeviceName, 'pinCount' => $TotalPinCount, 'startDate' => $Start_Date, 'endDate' => $DeliveryDate, 'customer' => $Customer ));

En lo anterior $DeviceName, $Start_Date, $Customer, $TotalPinCount, $NeedleType, $DeliveryDate tienen valores respectivos. Cuando hago echo , puedo ver los valores individuales.

Pero tengo Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) error al ejecutar esto. Leí que esto podría deberse a que está devolviendo cadenas vacías. Pero ese no es el caso. Cuando traté de inspeccionar el tipo de datos que regresan en Ajax con éxito usando alert(jQuery.type(fetchedDetails)) , muestra una cadena. ¿Alguien sabe lo que está mal aquí?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Debe devolver la matriz codificada JSON en lugar de eco. Puede usar typeOf() en jQuery para verificar el tipo de datos.

 return json_encode(array( 'needle' => $NeedleType, 'deviceName' => $DeviceName, 'pinCount' => $TotalPinCount, 'startDate' => $Start_Date, 'endDate' => $DeliveryDate, 'customer' => $Customer ));
over 4 years ago · Santiago Trujillo Report

0

Creo que hay algún problema con tu código. Simplemente escribe:

 echo json_encode([ 'needle' => $NeedleType, 'deviceName' => $DeviceName, 'pinCount' => $TotalPinCount, 'startDate' => $Start_Date, 'endDate' => $DeliveryDate, 'customer' => $Customer ]);

No necesita empujar matriz, etc.

Para su interfaz, intente con console.log (fechadoDetalles)

 $("#grabIdCodeDetails").click(function() { var idCodeForDetails = $("#idCode").val(); if (idCodeForDetails.trim() == "") { errorFunction("Please key in an ID Code"); hidePreviousShownResult(); } else { $.ajax({ type: "POST", url: "fetchIdCodeDetailsFromDatabaseAjax.php", data: { idCodeForDetails: idCodeForDetails }, success: function(fetchedDetails) { console.log(fetchedDetails) // here }, error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.status); console.log(thrownError); } }); } });

La alerta no mostrará los detalles.

Después de eso, verifique sus fetchedDetails y luego use

JSON.parse(fetchedDetails)

o

JSON.parse(fetchedDetails.data)

Es posible que tenga algunos códigos de estado, etc.

over 4 years ago · Santiago Trujillo 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!