Comment obtenir le tableau de PHP à JAVASCRIPT via AJAX et JSON?

Est-ce que quelqu’un peut m’aider? Il semble que je reçois une chaîne au lieu d’un tableau. Une fois que je passe le tableau, résultat d’une requête SQL, de PHP à Javascript en passant par AJAX, je voudrais boucler le tableau et renseigner des champs dans un formulaire. Je poste le code:

JAVASCRIPT

(function($){ $(document).ready(function() { $('#input_12_153').change(function (){ if ($('#input_12_153').attr("value")== 'no-selection'){ $('#input_12_48').val( '' );} else{ var valor = $('#input_12_153').attr("value"); jQuery.ajax({ // We use jQuery instead $ sign, because WordPress convention. url : '/optcat/wp-admin/admin-ajax.php', // This addres will redirect the query to the functions.php file, where we coded the function that we need. type : 'POST', data : { action : 'my_action', fieldvalue : valor, }, success: function(data) { alert( data ); //Here I would like to loop the array and get the values } }); } }); }); })(jQuery); 

PHP (Functions.php)

 add_action( 'wp_ajax_my_action', 'my_action' ); add_action( 'wp_ajax_nopriv_my_action', 'my_action' ); // This lines it's because we are using AJAX on the FrontEnd. function my_action(){ global $wpdb; $tablename = $wpdb->prefix . 'rg_lead_detail'; $lead_id = $_POST['fieldvalue']; // This variable will get the POST 'fieldvalue' $form_id = 21; $sql = "SELECT value FROM $tablename WHERE lead_id = %d AND form_id= %d"; $results = $wpdb->get_results( $wpdb->prepare( $sql, $lead_id, $form_id ), ARRAY_A ); echo json_encode($results,JSON_FORCE_OBJECT); } 

Utilisez JSON.parse () pour transformer la chaîne de PHP en tableau.

 success: function(data) { var results = JSON.parse(data); // like this alert( data ); //Here I would like to loop the array and get the values } 

Dans mon cas, il n’était pas nécessaire d’utiliser JSON.parse(data); dataType: "json" encore, j’avais une erreur de type “Jeton inattendu o dans JSON en position 1 à” en ne mettant que dataType: "json" est suffisant. Je poste mon code final:

functions.php

 add_action( 'wp_ajax_my_action', 'my_action' ); add_action( 'wp_ajax_nopriv_my_action', 'my_action' ); // This lines it's because we are using AJAX on the FrontEnd. function my_action(){ global $wpdb; $tablename = $wpdb->prefix . 'rg_lead_detail'; $lead_id = $_POST['fieldvalue']; // This variable will get the POST 'fieldvalue' $form_id = 21; $sql = "SELECT value FROM $tablename WHERE lead_id = %d AND form_id= %d"; $results = $wpdb->get_results( $wpdb->prepare( $sql, $lead_id, $form_id ), ARRAY_A ); echo json_encode($results); die(); } 

Javascript

 (function($){ $(document).ready(function() { $('#input_12_153').change(function (){ if ($('#input_12_153').attr("value")== 'no-selection'){ $('#input_12_48').val( '' );} else{ var valor = $('#input_12_153').attr("value"); jQuery.ajax({ // We use jQuery instead $ sign, because WordPress convention. url : '/optcat/wp-admin/admin-ajax.php', // This addres will redirect the query to the functions.php file, where we coded the function that we need. type : 'POST', data : { action : 'my_action', fieldvalue : valor, }, dataType:'json', success: function(data) { //var results = JSON.parse(data); // like this $(data).each(function(i,val){ $.each(val,function(k,v){ alert(k+" : "+ v); }); }); } }); } }); }); })(jQuery);