我在字体结尾有这个表格:
<form id="some_info" method="post" action="#">
<input type="text" name="info" id="info" value="">
<input type="hidden" name="action" id="action" value="my_down_action">
<input type="file" name="test">
<input id="submit-ajax" name="submit-ajax" type="submit" value="dwonload">
<form>
当点击提交时,ajax通过以下代码将表单发送到php函数my_down_action
:
var options = {
success: showResponse,
url: ajaxurl
};
// bind form using 'ajaxForm'
$('#some_info').ajaxForm(options);
function showResponse(responseText, statusText, xhr, $form) {
//download the text
}
这是php
函数:
//hook the Ajax call
//for logged-in users
add_action('wp_ajax_my_down_action', 'my_down_action');
//for none logged-in users
add_action('wp_ajax_nopriv_my_down_action', 'my_down_action');
function my_down_action(){
$ttext = $_POST['info'];
header('Content-Type: application/txt');
header('Content-Disposition: attachment; filename=info.txt');
header('Pragma: no-cache');
echo $ttext;
die();
}
如何在ajax中下载响应?