views:

60

answers:

2

Hi. I need (recently) to get an array from the server after an ajax call created by jquery. I know that i can do it using JSON. But i don't know how to implement it with JQuery (im new with JSON). I try to search in internet some example, but i didnt find it.

This is the code :

// js-jquery function
function changeSponsor() {
    $.ajax({
        type: 'POST',
        cache: false,
        url: './auth/ajax.php',
        data: 'id=changespon',
        success: function(msg) {
            // here i need to manage the JSON object i think
        }
    });
    return false;
}

// php-server function
if((isset($_POST['id'])) && ($_POST['id']=="changespon")) {
    $linkspon[0]="my ";
    $linkspon[1]="name ";
    $linkspon[2]="is ";
    $linkspon[3]="marco!";

    echo $linkspon;
}

in fact, i need to get the array $linkspon after the ajax call and manage it. How can do it? I hope this question is clear. Thanks

EDIT

ok. this is now my jquery function. I add the $.getJSON function, but i think in a wrong place :)

function changeSponsor() {
    $.ajax({
        type: 'POST',
        cache: false,
        url: './auth/ajax.php',
        data: 'id=changespon',
        dataType: 'json',
        success: function(data) {
            $.getJSON(url, function(data) { alert(data[0]) } ); 
        }       
    });

    return false;
}
+2  A: 

1) PHP: You need to use json_encode on your array.

e.g.

// php-server function 
if((isset($_POST['id'])) && ($_POST['id']=="changespon")) { 
    $linkspon[0]="my "; 
    $linkspon[1]="name "; 
    $linkspon[2]="is "; 
    $linkspon[3]="marco!"; 

    echo json_encode($linkspon); 
}

2) JQUERY:

use $.getJSON(url, function(data) {   whatever.... } );

Data will be passed back in JSON format. IN your case, you can access data[0] which is "my";

seatoskyhk
ive tried to use this code, but i think im using it in the wrong place. Check it out the code above ;) thanks for the help
markzzz
+2  A: 

Two things you need to do.

  1. You need to convert your array to JSON before outputting it in PHP. This can easily be done using json_encode, assuming you have a recent version of PHP (5.2+). It also is best practice for JSON to use named key/value pairs, rather than a numeric index.
  2. In your jQuery .ajax call, set dataType to 'json' so it know what type of data to expect.

    // JS/jQuery
    function changeSponsor() {
        $.ajax({
            type: 'POST',
            cache: false,
            url: './auth/ajax.php',
            data: 'id=changespon',
            dataType: 'json',
            success: function(data) {
                console.log(data.key); // Outputs "value"
                console.log(data.key2); // Outputs "value2"
            }
        });
        return false;
    }
    
    
    // PHP
    if((isset($_POST['id'])) && ($_POST['id']=="changespon")) {
        $linkspon["key"]= "value";
        $linkspon["key2"]= "value2";
        echo json_encode($linkspon);
    }
    
Brad Daily
yeah...this rocks!! i just send an array with 0,1,....n index and i manage it on jquery with data[0], data[1], ... data[n] ! thanks man :)
markzzz