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

115
Views
Analizar JSON de JQuery.ajax datos de éxito

Tengo problemas para obtener el contenido del objeto JSON de una llamada JQery.ajax. Mi llamada:

 $('#Search').click(function () { var query = $('#query').valueOf(); $.ajax({ url: '/Products/Search', type: "POST", data: query, dataType: 'application/json; charset=utf-8', success: function (data) { alert(data); for (var x = 0; x < data.length; x++) { content = data[x].Id; content += "<br>"; content += data[x].Name; content += "<br>"; $(content).appendTo("#ProductList"); // updateListing(data[x]); } } }); });

Parece que el objeto JSON se devuelve correctamente porque "alerta (datos)" muestra lo siguiente

 [{"Id": "1", "Name": "Shirt"}, {"Id": "2", "Name":"Pants"}]

pero cuando trato de mostrar la identificación o el nombre en la página usando:

 content = data[x].Id; content += "<br>"; content += data[x].Name; content += "<br>";

devuelve "indefinido" a la página. ¿Qué estoy haciendo mal?

Gracias por la ayuda.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Los datos regresan como la representación de cadena del JSON y no los vuelve a convertir en un objeto de JavaScript. Establezca el tipo de datos en solo ' dataType 'json' para que se convierta automáticamente.

over 4 years ago · Santiago Trujillo Report

0

Te recomiendo que uses:

 var returnedData = JSON.parse(response);

para convertir la cadena JSON (si es solo texto) en un objeto JavaScript.

over 4 years ago · Santiago Trujillo Report

0

Funciona bien, Ej:

 $.ajax({ url: "http://localhost:11141/Search/BasicSearchContent?ContentTitle=" + "تهران", type: 'GET', cache: false, success: function(result) { // alert(jQuery.dataType); if (result) { // var dd = JSON.parse(result); alert(result[0].Id) } }, error: function() { alert("No"); } });

Finalmente, necesita usar esta declaración ...

 result[0].Whatever
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!