我无法使用WordPress AJAX在jsGrid上获得结果。它正在渲染表格的空白框架。
$("#orders-grid").jsGrid({
height: "auto",
width: "100%",
filtering: true,
sorting: true,
paging: true,
autoload: true,
pageSize: 10,
pageButtonCount: 5,
controller: {
loadData: function (filter) {
return $.ajax({
type: "GET",
url: ajax_vars.ajax_url,
dataType: "json",
data: {
action: 'gs_orders_grid',
nonce: ajax_vars.nonce
},
beforeSend: function () {
console.log('Getting...');
},
success: function (response) {
console.log(response);
},
error: function (response) {
console.log('Error: ' + response);
},
complete: function () {
console.log('Done...');
}
})
}
},
fields: [
{name: "id", title: "ID"},
{name: "order_number", title: "Order Number"},
{name: "status", title: "Status"},
{name: "created_at", title: "Created At"},
]
});
function gs_enqueue_orders_grid_ajax_scripts()
{
wp_register_script('gs-orders-grid-ajax', GROUP_SHOP_ROOT . 'public/js/orders-grid-ajax.js', ['jquery'], 1.0, TRUE);
wp_localize_script('gs-orders-grid-ajax', 'ajax_vars', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('gs_nonce'),
]);
wp_enqueue_script('gs-orders-grid-ajax');
}
// hook the function
add_action('wp_enqueue_scripts', 'gs_enqueue_orders_grid_ajax_scripts');
/**
* AJAX form processor to update product in cart
*/
function gs_orders_grid_ajax()
{
// Check if user is logged in
if ( ! is_user_logged_in()) {
// throw error if user is not logged in
wp_send_json_error(__('Please login to order', 'group-shop'));
// we use this manually as want to print error
wp_die();
}
// check and validate nonce
if ( ! check_ajax_referer('gs_nonce', 'nonce', FALSE)) {
// throw error if validation fails
wp_send_json_error(__('Do not be nasty with validation', 'group-shop'));
// we use this manually as want to print error
wp_die();
}
$orders = new Group_Shop_Order();
$items = $orders->get_orders();
$data = [];
$order_items = [];
foreach ($items as $item) {
$data[ 'id' ] = $item->id;
$data[ 'order_number' ] = $item->order_number;
$data[ 'status' ] = $item->status;
$data[ 'created_at' ] = $item->created_at;
$order_items[] = $data;
}
echo json_encode($order_items);
}
// hook the function
add_action('wp_ajax_gs_orders_grid', 'gs_orders_grid_ajax');
add_action('wp_ajax_nopriv_gs_orders_grid', 'gs_orders_grid_ajax');