How to return an array from an AJAX call?

前端 未结 6 1397
猫巷女王i
猫巷女王i 2020-12-02 06:13

I\'m searching for a better solution to making an AJAX call with jQuery, having the PHP file return an array, and have it come out client-side as a Javascript array. Here is

相关标签:
6条回答
  • 2020-12-02 06:56

    Php has a super sexy function for this, just pass the array to it:

    $json = json_encode($var);
    
    $.ajax({
    url:"Example.php",
    type:"POST",
    dataType : "json",
    success:function(msg){
        console.info(msg);
    }
    });
    

    simples :)

    0 讨论(0)
  • 2020-12-02 07:10

    well, I know that I'm a bit too late, but I tried all of your solutions and with no success! So here is how I managed to do it. First of all, I'm working on an Asp.Net MVC project. The Only thing I changed was in my c# method getInvitation:

    public ActionResult getInvitation (Guid s_ID)
    {
        using (var db = new cRM_Verband_BWEntities())
        {
            var listSidsMit = (from data in db.TERMINEINLADUNGEN where data.RECID_KOMMUNIKATIONEN == s_ID select data.RECID_MITARBEITER.ToString()).ToArray();
            return Json(listSidsMit);
        }
    }
    

    SuccessFunction in JS :

    function successFunction(result) {
        console.log(result);
    }
    

    I changed the Method Type from string[] to ActionResult and of course at the end I wrapped my array listSidsMit with the Json method.

    0 讨论(0)
  • 2020-12-02 07:12

    Have a look at json_encode() in PHP. You can get $.ajax to recognize this with the dataType: "json" parameter.

    0 讨论(0)
  • 2020-12-02 07:13

    Have a look at json_encode (http://php.net/manual/en/function.json-encode.php). It is available as of PHP 5.2. Use the parameter dataType: 'json' to have it parsed for you. You'll have the Object as the first argument in success then. For further information have a look at the jQuery-documentation: http://api.jquery.com/jQuery.ajax/

    0 讨论(0)
  • 2020-12-02 07:14

    @Xeon06, nice but just as a fyi for those that read this thread and tried like me... when returning the array from php => json_encode($theArray). converts to a string which to me isn't easy to manipulate esp for soft js users like myself.

    Inside js, you are trying to get the array values and/or keys of the array u r better off using JSON.parse as in var jsArray = JSON.parse(data) where data is return array from php. the json encoded string is converted to js object that can now be manipulated easily.

    e.g. foo={one:1, two:2, three:3} - gotten after JSON.parse

    for (key in foo){ console.log("foo["+ key +"]="+ foo[key]) } - prints to ur firebug console. voila!

    0 讨论(0)
  • 2020-12-02 07:16

    Use JSON to transfer data types (arrays and objects) between client and server.

    In PHP:

    • json_encode
    • json_decode

    In JavaScript:

    • JSON.stringify
    • JSON.parse

    PHP:

    echo json_encode($id_numbers);
    

    JavaScript:

    id_numbers = JSON.parse(msg);
    

    As Wolfgang mentioned, you can give a fourth parameter to jQuery to automatically decode JSON for you.

    id_numbers = new Array();
    $.ajax({
        url:"Example.php",
        type:"POST",
        success:function(msg){
            id_numbers = msg;
        },
        dataType:"json"
    });
    
    0 讨论(0)
提交回复
热议问题