开发者

JQuery + Json - first steps with an example

开发者 https://www.devze.com 2023-01-22 02:50 出处:网络
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 J开发者_Go百科Query (im new with

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 J开发者_Go百科Query (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;
}


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


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";

0

精彩评论

暂无评论...
验证码 换一张
取 消