How can i do something like it:
$(document).ready(function() {
$('a').click(function() {
jQuery.ajax({
url: 'test.php',
async: false,
success: function(data) {
/* here */
}
});
});
})
;
test.php:
<?php
header("Content-Type: application/csv");
header("Content-Disposition: attachment; filename=file.csv");
echo "field1,field2,field3";
?>
i need to simulate a download with the jquery ajax result... it's possible to do?
Why do you wish to do it with ajax? You can use direct link, for example:
<a href="getmyfile.php?id=1">Load .csv file</a>
And write content of desired file direct to output (with correct header).
Or do you want to do something with content of this file after loading? in this case, try:
$(document).ready(function() {
$('a').click(function() {
jQuery.ajax({
url: 'somefile.csv',
async: true,
success: function(data) {
/* here */
}
});
});})
If you need to get some javascript-object from server with ajax you can use JSON:
{
someField: <?php echo $myfield?>,
someArray: [<?php echo $someNumericValue?>, '<?php someStringValue?>']
}
upd:
<html>
<head>
<title>Basic JavaScript/AJAX Example</title>
<script type="text/javascript" src="jquery-1.4.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {$('a').click(function() {
jQuery.ajax({
url: 'somefile.csv',
async: true,
success: function(data) {
$('#myTextArea').html(data);
}
});
});
});
</script>
</head>
<body>
<h1>Basic JavaScript/AJAX Example</h1>
<a href="#">Click Here to load the contents of a page into the TextArea below</a><br />
<textarea id="myTextArea" rows="30" cols="70"></textarea>
</body>
</html>
where content of somefile.csv is: test1, test2, test3
after click on 'a' content of 'omefile.csv' placed into textarea
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.