我正在尝试通过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
的期望如何?