如何通过AJAX和JSON从PHP到JAVASCRIPT获取数组?

时间:2017-05-05 08:07:14

标签: jquery json ajax wordpress

有人可以帮帮我吗?好像我得到一个字符串而不是一个数组。 一旦我通过AJAX传递数组,这是sql查询的结果,从PHP到Javascript,我想循环数组并填充表单中的一些字段。 我发布了代码:

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);
} 

2 个答案:

答案 0 :(得分:0)

使用JSON.parse()将字符串从PHP转换为数组。

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

答案 1 :(得分:0)

在我的情况下,没有必要再次使用JSON.parse(data);,否则我曾经在“位置1处的JSON中出现意外令牌o” 仅仅dataType: "json"就足够了。 我发布了我的最终代码:

的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);