【发布时间】:2016-10-26 17:55:14
【问题描述】:
我正在尝试通过 post 将一些数据发送到 php 文件,但由于某种原因它没有被发布。我从var_dump 上的 php 文件在控制台日志中收到一个空数组。
这是我在这些文件中的代码:
index.php
<button type="button" class="btn btn-warning" id="resetPassword">Reset Password</button>
<script>
$(document).ready(function (e){
$("#resetPassword").click(function(e){
e.preventDefault();
$.ajax({
url: "scripts/process.php",
type: "POST",
data: {name: 'test'},
cache: false,
processData:false,
success: function(data) {
if (data === 'Success') {
new PNotify({
title: 'Success!!!',
text: 'Request completed successfully.',
type: 'success',
styling: 'bootstrap3'
});
$('#exampleModal').modal('hide');
$('#datatable-buttons').DataTable().ajax.reload();
} else {
console.log(data);
new PNotify({
title: 'Error!!!',
text: data,
type: 'error',
styling: 'bootstrap3'
});
//document.getElementById("status").className += " alert-danger";
//$("#status").html(data);
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert("ERROR:" + xhr.responseText+" - "+thrownError);
}
});
});
});
</script>
这里是php文件process.php。
var_dump($_POST);
console.log 中的结果:
array(0) {}
解决方案:
删除processData: false
谢谢大家。
【问题讨论】:
-
为什么你甚至期望
data是Success?它不能像你那样工作。data是你 php 的var_dump($_POST);的内容,而不是Success。 -
并删除
processData: false,那么您的参数也应该可以工作。 -
请检查您的数据是否返回成功,因为您正在尝试使用
===进行检查,因为它将检查更准确的结果并检查console.log(data)。在您的 php 文件中检查返回值。满足条件后退出,