开发者

JSON object being passed using JQUERY AJAX cannot be accessed

I am sending a json string using JQuery.ajax to a php page where I am trying to pull the array and mediate through the variables. For some reason, I can see the POST in firebug when I run the script but I when I attempt to get the parameters all I get is an empty Array(). I have been trying to figure this out for hours and am at my wits end. I moving from asp.net to .php so I am still a little fresh with my syntax, but this seems like it should be fairly simple.

Here is my code for the jquery send.

$('#form_add').submit(function() {
    var serial_data = $(this).serialize();
    $(".page_content").fadeOut("slow", function(){
        $.get(uri_base +'/'+uri_cont+'/get_db_name/true/'+Math.random(), function(data) { 
            $.ajax({
                type: 'POST',
                url: uri_base +'/AJAX/add_record/'+data+'/'+Math.random(),
                data: serial_data,
                contentType: "application/json",
                success: function(data){
                    $.ajax({
                        type: 'POST',
                        url: uri_base +"/"+ uri_cont +"/"+ uri_func,
                        data: data,
                        success: function(data){
                            alert(data)


                        },
                        error: function(xhr, ajaxOptions, thrownError){
                             alert(xhr.status);
                             alert(thrownError);
                        }

                    });  

                },
                error: function(xhr, ajaxOptions, thrownError){
                     alert(xhr.status);
                     alert(thrownError);
                }

            });

        });                       
    });
    return false;
});

Ignore the first .get(), that just pulls back a variable to complete the next URL. The first.ajax() call sends the serialized form data to my AJAX controller and pushes into the db. The return from the controller sends back an Array of Objects that I converted to json.

if(isset($key)){    
    $data['add_msg'] = $this开发者_StackOverflow社区->model_ajax->add_record($table,$array);
    $exploded_data = json_encode($data['add_msg']);
    print_r($exploded_data);
    exit;
}

From there the next .ajax() is supposed to send the data to my controller to pull back a view to display in the .pag_content div. I have tried echoing the json object, a for each loop to pull the keys out, and a myriad of other things. nothing ... I get either a JSON Invalid error (when trying to decode it), Just "Array()" output, or nothing at all when looping through them.

This is the controller code and the POST output that I am seeing in firebug...

echo $_POST;

$array = $_POST;
echo $array;

foreach($_POST as $key){
    echo $key;
}
exit;

[{"id":"29","datetime":"2011-03-23 12:10:25","full_name":"Leroy Brown","email_address":"test@testing.com","password":"asdf","id_group":"0","id_sites":"0","active":"0"}]


before you output anything in php make sure you send the right content-type. The frameworks like JQuery and mootools tend to filter (as they should) with Accept charsets (you can see that in firebug)

header("Content-Type: application/json"); // could also be text/json, check the accept headers in firebug depending on your framework

:)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜