无法访问使用 JQUERY AJAX 传递的 JSON 对象

发布于 2024-10-25 12:00:38 字数 2480 浏览 2 评论 0原文

我正在使用 JQuery.ajax 将 json 字符串发送到 php 页面,在该页面中我尝试拉取数组并通过变量进行调解。由于某种原因,当我运行脚本时,我可以在 firebug 中看到 POST,但是当我尝试获取参数时,我得到的只是一个空的 Array()。我几个小时以来一直试图解决这个问题,但我却束手无策。我从 asp.net 迁移到 .php,所以我对我的语法仍然有点新鲜,但这看起来应该相当简单。

这是我的 jquery 发送代码。

$('#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;
});

忽略第一个 .get(),它只是拉回一个变量来完成下一个 URL。 first.ajax() 调用将序列化的表单数据发送到我的 AJAX 控制器并推送到数据库中。控制器的返回会发回一个我转换为 json 的对象数组。

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

从那里开始,下一个 .ajax() 应该将数据发送到我的控制器,以拉回视图以显示在 .pag_content div 中。我尝试过回显 json 对象、foreach 循环来拉出密钥,以及无数其他的东西。什么都没有......我要么得到一个 JSON Invalid 错误(当尝试解码它时),要么得到“Array()”输出,要么在循环它们时什么也没有得到。

这是我在 firebug 中看到的控制器代码和 POST 输出...

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":"[email protected]","password":"asdf","id_group":"0","id_sites":"0","active":"0"}]

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->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":"[email protected]","password":"asdf","id_group":"0","id_sites":"0","active":"0"}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫雨熙 2024-11-01 12:00:38

在 php 中输出任何内容之前,请确保发送正确的内容类型。像 JQuery 和 mootools 这样的框架倾向于使用 Accept 字符集进行过滤(正如它们应该的那样)(你可以在 firebug 中看到这一点)

header("内容类型:application/json");
// 也可以是 text/json,根据您的框架检查 firebug 中的接受标头

:)

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

:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文