错误的请求400。jQuery Ajax数据serialize()或json的外观如何?

时间:2019-02-21 03:54:02

标签: php jquery ajax

我正在尝试通过jquery ajax发送表单。我有这个:

JS:

$('#form-pm').on('submit',function(e) {

        e.preventDefault();

        var formData = $('#form-pm').serialize();
        var ajaxRequest =
        $.ajax({
            url: //my valid url,
            dataType: 'json',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            type: 'post',
            data: formData    
        });

        ajaxRequest.done(function(data) { console.log(data); });
        ajaxRequest.fail(function(jqXHR) { alert('You are fail. ' + jqXHR); });

    });

更新 JS现在看起来像这样:

var formData = $('#form-pm').serialize();

        var ajaxRequest =
        $.ajax({
            url: admin_ajax.ajax_url,
            type: 'post',
            data: formData

        });

        ajaxRequest.done(function(data) { console.log(data); });
        ajaxRequest.fail(function(jqXHR) { alert('You are fail. ' + jqXHR); });
        e.preventDefault();

400错误请求消失了,但是我的函数send_message接收到空白参数。根据error.log,它是send_message('',NULL,NULL)

PHP:

function send_message($projectid, $userid, $message) {
    //do stuff
}

<form id="form-pm" method="post" enctype="multipart/form-data" action="">
    <textarea name="message" rows=3 id="project-message"></textarea>

    <input type="submit" name="send" value="send message">

    <input id="userid" type="hidden" name="userid" value="<?php echo $userid; ?>">
    <input id="projectid" type="hidden" name="projectid" value="<?php echo $projectid; ?>">
    <input id="userid" type="hidden" name="action" value="send_message">

</form>

我不断收到POST 400错误请求。我知道是data造成了麻烦。帖子对data的期望如何?

0 个答案:

没有答案