Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
var_dump($_POST)为通过jQuery AJAX传递的字符串化JSON返回数组(0){}_Jquery_Ajax_Post_Typescript_Stringify - Fatal编程技术网

var_dump($_POST)为通过jQuery AJAX传递的字符串化JSON返回数组(0){}

var_dump($_POST)为通过jQuery AJAX传递的字符串化JSON返回数组(0){},jquery,ajax,post,typescript,stringify,Jquery,Ajax,Post,Typescript,Stringify,嗨,我好像没法让这个职位发挥作用。首先,我使用Typescript创建了一个类: class ParticipantInfo { constructor (public fname: string, public mname: string, public lname: string, public ssn: string, public sin: string,

嗨,我好像没法让这个职位发挥作用。首先,我使用Typescript创建了一个类:

class ParticipantInfo {

constructor (public fname: string,
             public mname: string,
             public lname: string,
             public ssn: string,
             public sin: string,
             public tin: string,
             public addr1: string,
             public addr2: string,
             public addr3: string,
             public city: string,
             public state: string,
             public zip: string,
             public email: string,
             public phone: string,
             public local: string){}
}
是谁创建了此javascript:

var ParticipantInfo = (function () {
    function ParticipantInfo(fname, mname, lname, ssn, sin, tin, addr1, addr2, addr3, city, state, zip, email, phone, local) {
    this.fname = fname;
    this.mname = mname;
    this.lname = lname;
    this.ssn = ssn;
    this.sin = sin;
    this.tin = tin;
    this.addr1 = addr1;
    this.addr2 = addr2;
    this.addr3 = addr3;
    this.city = city;
    this.state = state;
    this.zip = zip;
    this.email = email;
    this.phone = phone;
    this.local = local;
    }
    return ParticipantInfo;
}());
我使用jQuery处理附加到表单元素的HTML按钮上的提交事件:

$(document).ready(function(){
    $("#frmInquiryForm").submit(function(event){

        event.preventDefault();

        var formData = new ParticipantInfo($('[name="f1_fname"]').val(),
                                           $('[name="f1_mname"]').val(),
                                           $('[name="f1_lname"]').val(),
                                           $('[name="f1_ssn"]').val(),
                                           $('[name="f1_sin"]').val(),
                                           $('[name="f1_tin"]').val(),
                                           $('[name="f1_addr1"]').val(),
                                           $('[name="f1_addr2"]').val(),
                                           $('[name="f1_addr3"]').val(),
                                           $('[name="f1_city"]').val(),
                                           $('[name="f1_state"]').val(),
                                           $('[name="f1_zip"]').val(),
                                           $('[name="f1_email"]').val(),
                                           $('[name="f1_phone"]').val(),
                                           $('[name="f1_local"]').val());
$.ajax({
            type: "POST",
            url:"processForm.php",
            data: JSON.stringify(formData),
            success: function(data){
                alert(data);
                window.location = 'confirmForm.php?message='+data;
            },
            error: function(xhr, ajaxOptions, thrownError){
                alert('status='+ajaxOptions);
                alert("XHR STATUS="+xhr.status+" "+ thrownError);

            }

        });

    })
})
为了进行测试,我在processForm.php页面上所做的全部工作如下:

<?php
    var_dump($_POST);
?>


来自成功AJAX调用的警报消息仅返回“array(0){}”。是JSON.stringify的问题还是有其他人知道的东西?提前感谢您的帮助

在发送数据之前删除
JSON.stringify

您没有在
$调用中指定
contentType
。ajax
调用中,文档说默认值是
application/x-www-form-urlencoded
。如果向jquery传递对象,jquery将数据编码为
foo=bar&this=that
,但这里传递的是编码的json字符串,从而导致意外行为,对我来说,这可能是php接收类似
“{”foo:“bar”}”
,而php只自动解析
urlcoded
请求体(因为json格式的正文在标准中相对较新)


因此,如果您想使用
应用程序/json
内容类型
,请确保在
$.ajax
的选项中指定它,并像
$requestArray=json\u decode(file\u get\u contents('php://input'))
。阅读另一个问题中的更多内容。

在发送数据之前删除
JSON。stringify

您没有在
$调用中指定
contentType
。ajax
调用,文档说默认值是
application/x-www-form-urlencoded
。jquery将您的数据编码为
foo=bar&this=that
,如果您向它传递一个对象,但在这里传递一个编码的json字符串,从而导致意外行为,对我来说,这是可能php会收到类似于
“{”foo:“bar”}”
,而php只自动解析
urlencoded
请求体(因为json格式的体在标准中相对较新)


因此,如果您想使用
应用程序/json
内容类型
,请确保在
$.ajax
的选项中指定它,并像
$requestArray=json\u decode(file\u get\u contents('php://input);
。请阅读另一个问题中的更多内容。

谢谢潘俊杰提供的信息潘俊杰! 我之所以去字符串化对象,是因为我还有该函数的另一个方法来执行客户端验证。当我直接传递对象时,它会传递该方法并运行它。函数的参数不会像我通常直接调用函数时那样传递给方法,因此验证失败(当它正常通过时)。我将尝试你给出的第二个建议并阅读另一个问题。感谢你的帮助!感谢潘俊杰提供的信息潘俊杰! 我之所以去字符串化对象,是因为我还有该函数的另一个方法来执行客户端验证。当我直接传递对象时,它会传递该方法并运行它。函数的参数不会像我通常直接调用函数时那样传递给方法,因此验证失败(当它正常通过时)。我将尝试你给出的第二个建议并阅读另一个问题。谢谢你的帮助!