Use jQuery to send Excel data using AJAX

前端 未结 4 541
既然无缘
既然无缘 2020-12-13 18:13

I have the following function that is pulling data from a database. The ajax call is working correctly. How can I send the tab delimited data in my success function to the

相关标签:
4条回答
  • 2020-12-13 18:40

    Since it uses JavaScript, AJAX is bound by JavaScript's designed limitations, which includes interacting with other processes on the client's machine. In this case, it's a good thing; you wouldn't want a site to be able to automatically load an Excel document with a malicious macro in it.

    If you want to display the data in the browser, you can use AJAX; otherwise, you'll want to just give a link to an Excel document and let the browser's regular download handling capabilities figure out what to do.

    0 讨论(0)
  • 2020-12-13 18:45

    AJAX is... the wrong choice. Redirect the user to a server resource that will send the data down with the proper MIME type, and let the browser figure out what to do with it.

    0 讨论(0)
  • 2020-12-13 18:49

    It's possible that you don't want to do this with javascript.

    What I think you want to do is create a response page with the mine type application/csv then redirect the user to that page. I would probably do a window.open() since the user doesn't lose the page they're currently on.

    0 讨论(0)
  • 2020-12-13 18:52

    in HTML I have a Form with serial inputs elements and a button that calls a JavaScript function onclick="exportExcel();


    then in JavaScript file:

    function exportExcel(){
        var inputs = $("#myForm").serialize();
        var url = '/ajaxresponse.php?select=exportExcel&'+inputs;
        location.href = url;
    }
    

    and finally a pivot file who response to something

    PHP code:

    case 'exportExcel':{
                         ob_end_clean();
                         header("Content-type: application/vnd.ms-excel");
                         header("Content-Disposition: attachment;
                         filename=exportFile.xls");
                         echo $html->List($bd->ResultSet($_GET));
                    }
    

    $html is an object who handle html, and $bd is an object that returns data from Database send your own html table or whatever you want.

    0 讨论(0)
提交回复
热议问题