jQuery Serialize()在Ajax POST中不起作用

时间:2014-05-11 13:13:00

标签: jquery json post serialization

这是我的AJAX请求

$(document).ready(function(){
        $("#register-submit").click(function(){
        var formdata = {hotelname: $('#hotelName').val(), contactType: $('#contactType').val(), contactNumber: $('#contactNumber').val(), addrOne: $('#addrLineOne').val(), addrTwo: $('#addrLineTwo').val(),cityName: $('#cityName').val(), stateName: $('#stateName').val(),localityName: $('#localityName').val(), pincode: $('#pincode').val(), managerName: $('#mngrName').val(), managerEmail: $('#mngrEmail').val(), managerPhone: $('#mngrPhone').val()}
        jQuery.ajax({
                url: "../api/v1/admin/ch_partialBusinessRegister.php",
                data: JSON.stringify(formdata),
                type: "POST",
                async: false,
                success: function(res) {
                    var result = res.Result;
                    if(result.success === true)
                        {
                            alert("Random Password is Generated : "+res.Result.password);
                            window.location.reload();
                        }
                        else
                            alert("Registration Failed. "+res.Result.msg);
                }
            });

    });
});

在我的ch_partialBusinessRegister.php

$inputJson = file_get_contents('php://input');
$post_vars = json_decode($inputJson, true);
$businessName = $post_vars['hotelname'];
$address1 = $post_vars['addrOne'];
$address2 = $post_vars['addrTwo'];
$locality = $post_vars['localityName'];
$city = $post_vars['cityName'];
$state = $post_vars['stateName'];
$zip = $post_vars['pincode'];
$mName = $post_vars['managerName'];
$mEmail = $post_vars['managerEmail'];
$mPhone = $post_vars['managerPhone'];

能够获取数据并成功将其传递给POST参数。但是当我将formdata更改为

$(document).ready(function(){
            $("#register-submit").click(function(){
            var formdata = $('form').serializeArray();
            jQuery.ajax({
                    url: "../api/v1/admin/ch_partialBusinessRegister.php",
                    data: JSON.stringify(formdata),
                    type: "POST",
                    async: false,
                    success: function(res) {
                        var result = res.Result;
                        if(result.success === true)
                            {
                                alert("Random Password is Generated : "+res.Result.password);
                                window.location.reload();
                            }
                            else
                                alert("Registration Failed. "+res.Result.msg);
                    }
                });

        });
    });

然后JSON.stringify它,它通过像这样的

这样的POST参数发送变量
[{"name":"hotelName","value":"test"},{"name":"contactType","value":"LandLine"},{"name":"contactNumber[]","value":""},{"name":"AddrOne","value":"test"},{"name":"addrTwo","value":"test"},{"name":"pincode","value":"test"},{"name":"mngrName","value":"test"},{"name":"mngrEmail","value":"test"},{"name":"mngrPhone","value":"test"}]

但在PHP中,它并没有读取值。

如果我使用

,也一样
var mydata = $('form').serialize();
 data: JSON.stringify(mydata)

它发布此数据,但不会被PHP读取

"hotelName=&contactType=LandLine&contactNumber%5B%5D=&AddrOne=&addrTwo=&pincode=&mngrName=test&mngrEmail=test&mngrPhone=test"

2 个答案:

答案 0 :(得分:0)

只需通过你的myData'对象就是这样。不要把它串联起来。

答案 1 :(得分:0)

你可以添加这个

$(document).ready(function(){
        $("#register-submit").click(function(){
           jQuery.ajax({
           url: "../api/v1/admin/ch_partialBusinessRegister.php",
           type: 'post',
          data: {'inputJson': $('form').serialize()},
          dataType: 'json',
          success: function(res) {
                    var result = res.Result;
                    if(result.success === true)
                        {
                            alert("Random Password is Generated "+res.Result.password);
                            window.location.reload();
                        }
                        else
                            alert("Registration Failed. "+res.Result.msg);
                }
            });

    });
});

在你的php文件中你可以通过输入

来解码数组
$inputJson=$_POST['inputJson'];
$post_vars = json_decode($inputJson, true);

如果你想获得表格标签的价值

you can simply write

$tagValue=$post_vars['tagName']; // tag name inside the form which is sent using ajax