Например, когда я нажимаю «загрузить», он загружает данные с предыдущего щелчка.
Я думаю, что blob вызывает проблему, но, кажется, без него, невозможно заставить его работать с jQuery.
Вот мой php и ajax-код, который работает нормально, но я думаю, что есть кеширование на сервере.
$.ajax({
dataType: 'native',
url: printBulkWpApi.url,
type: "POST",
cache: false,
xhrFields: {
responseType: 'blob'
},
data: {
ids: data.join(","),
},
success: function (data) {
var link = document.createElement('a');
// Add the element to the DOM
link.setAttribute("type", "hidden"); // make it hidden if needed
link.download = "single_issues_print" + new Date() + ".pdf";
link.href = window.URL.createObjectURL(data);
document.body.appendChild(link);
link.click();
link.remove();
}
});
public function print_function_bulk_actions(){
function print_callback_bulk_actions (WP_REST_Request $request) {
global $wpdb;
require_once plugin_dir_path( __FILE__ ) . '../vendor/autoload.php';
$filename = "single_issues_print.pdf";
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1
header('Pragma: no-cache'); // HTTP 1.0
header('Expires: 0'); // Proxies
header("Content-Disposition: attachment; filename='.$filename.'");
ob_clean();
readfile($filename);
$ids = $_POST['ids'];
$ids_array = explode(",", $ids);
$array = [];
$mpdf = new \Mpdf\Mpdf();
foreach($ids_array as $id){
// echo $id;
$results = $wpdb->get_results("SELECT * FROM orders WHERE orders.id = $id");
$mpdf->WriteHTML('<h1>'.$results[0]->item_title.'</h1>');
$array[] = $results;
}
// var_dump($array);
$mpdf->Output('single_issues_print.pdf', \Mpdf\Output\Destination::FILE);
}
register_rest_route( 'order-center/v1', 'bulk_print/?', array(
'methods' => 'POST',
'callback' => 'print_callback_bulk_actions',
) );
}