dongzhuo1733 2015-12-11 00:26
浏览 211
已采纳

如何在jquery中将表单数据转换为JSON(初学者)?

I am trying to send data using AJAX to PHP page. I am getting all the data from the form and I am trying to convert it to JSON. However, .stringify() doesn't do the job for me.

Here is my code:

<script>
        $(document).ready(function(){

            console.log("Ready..");

            $("#profile-form").submit(function(event){
                var data = $(this).serialize();
                console.log(JSON.stringify(data));

                $.ajax({
                    type    : "POST",
                    url     : "profile.php",
                    data    : JSON.stringify(data),
                    success : function(response){
                        alert(response);
                    }
                });
            });

            //$("#profile-form").submit();
        });
</script>

I am able to see the form-data on the console. However, I am not getting any JSON data on the server. I have just done print_r($_POST['data']) in my profile.php page. However, it says variable data not defined.

  • 写回答

3条回答 默认 最新

  • drus39136 2015-12-11 00:32
    关注

    There should be no $_POST['data'] available because the data you are sending is saved directly in $_POST variable, which should be accessible with print_r($_POST) or print_r(json_decode(print_r($_POST))) (since you have stringified it.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部