Problems getting json array in php using json

Im trying to pass and return an array to and from a php script, I've tested the json_ecode portion and its working but I can't get the json_decode on the php side.

Javascript

    scid_list = []; 
    $('.filter_on').each(function(){scid_list.push($(this).data("scid"));});
    $.ajax({
       type: "POST",
       dataType: "json",
       data: {scid_list:scid_list},
       url: "/scripts/products_filter.php",
       success: function(data){
                alert(data.join('n'));
       }
    });

PHP

<?php
     $scid_list=(json_decode($_POST['scid_list'], true));
     echo json_encode($scid_list);
     exit();
?>

Ive also tried leaving out the true on decode

$scid_list=(json_decode($_POST['scid_list']);

and not decoding it at all

$scid_list=$_POST['scid_list'];

I'm not sure what I'm missing. I've tried playing around with serializing the data too but read I didn't have to if you specify the dataType as json, I tried using stripslashes

Any help is appreciated!

Cheers


I think the problem is that the data you're sending isn't json even though you're specifying it on the Ajax call. As it's only a 2-dimensional array you're getting, why don't you just use Array.join in your JS before doing the post:

var scids = scid_list.join(','); 

That would turn it in to a comma separated string. eg

"id1,id2..."

You can simplify the Ajax call a bit too:

$.post('/scripts/products_filter.php', {scids: scids}, function(data) {
    // process data response here
});

Then in your PHP file you can use explode() to turn it back in to an array:

$ids = explode(",",$_POST["scids"]);
foreach ($ids as $id) { 
    // Do something with the array of ids.    
};

Hope that all makes sense.


Your dataType parameter specifies that you expect JSON data in response, but it does not mean you are sending data as JSON. In your case you are sending an array, therefor in PHP instead of:

$scid_list=(json_decode($_POST['scid_list'], true)); /* WRONG */

you should simply use:

$scid_list=$_POST['scid_list'];

dataType (default: Intelligent Guess (xml, json, script, or html))

Type: String The type of data that you're expecting back from the server.

Alltogether, for given HTML:

<form action="work.php" method="post">
    <p><input class="filter_on" data-scid="22" type="checkbox" /> Filter 22</p>
    <p><input class="filter_on" data-scid="28" type="checkbox" /> Filter 28</p>
    <p><input class="filter_on" data-scid="31" type="checkbox" /> Filter 31</p>
    <p><input type="submit" value="Send" /></p>
</form>

and SCRIPT:

$(document).on('ready', function() {
    $('form').on('submit', function(e) {
        e.preventDefault();
        scid_list = []; 
        $('.filter_on:checked').each(function(i){
            scid_list.push( $(this).data("scid") );
        });
        $.ajax({
            type: "POST",
            dataType: "json",
            data: {scid_list:scid_list},
            url: "work.php",
            success: function(data){
                alert(data.join('n'));
            }
        });
    });
});

PHP part is:

$scid_list=$_POST['scid_list'];
echo json_encode($scid_list);
exit();
链接地址: http://www.djcxy.com/p/57676.html

上一篇: 如何找到命令行使用的php.ini文件?

下一篇: 使用json在php中获取json数组的问题