【发布时间】:2020-05-28 18:23:19
【问题描述】:
尝试使用 ajax 填充数据表。传递参数如下:
$('#submitDetails').on('click', function()
{
let details = $('#searchDetails').val();
$.ajax({
url: 'api/searchDetailInfo.php',
type: 'POST',
data: details,
dataType: 'html',
success: function(data, textStatus, jqXHR){
console.log(data); // <-- printing the return from the php script
let jsonObject = JSON.parse(data);
var table = $('#example1').DataTable({
"data": jsonObject,
"columns": [
{ "data": "COLUMN1" },
{ "data": "COLUMN2" },
{ "data": "COLUMN3" }
],
"iDisplayLength": 50,
"order": [[ 1, "desc" ]],
"paging": true,
"scrollY": 550,
"scrollX": true,
"bDestroy": true,
"stateSave": true,
"sPaginationType":"full_numbers",
"autoWidth": true
},
error: function(jqHHR, textStatus, errorThrown) {
$('#loadingDiv').hide();
$('#errorModal').modal('show');
$('.message').text('There was an error conducting your search.');
console.log('fail: '+ errorThrown);
return false;
}
});
我正在访问 PHP 脚本,但该脚本没有接收到参数。
<?php
include("../include/database.php");
if(isset($_POST['details']))
{
echo "good";
}
else
{
echo "bad";
}
?>
我只是在控制台中变得“糟糕”。我不确定为什么没有将参数发送到脚本。
我该如何解决这个问题?
【问题讨论】:
-
你试过
print_r($_POST);看看发送了什么。 -
@NigelRen - 测试...
-
我得到以下信息:数组([new_york] =>)
-
将
data: details,更改为data: {details:details},为 post 变量提供一个标识符。 -
完成 @JohnBeasley 很高兴能提供帮助!