开发者

jQuery.ajax data result question

How can i do something like it:

$(document).ready(function() {

    $('a').click(function() {

        jQuery.ajax({
            url: 'test.php',
                        async: false,
            success: function(data) {
                /* here */
            }
 开发者_JS百科       });

    });

})

;

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜