This article shares with you how to request to download an Execl file through Ajax. The content is very good. Friends in need can refer to it. I hope it can help everyone.
The problem of downloading Execl through Ajax request fell into a pit for more than half an hour, so I will record it here.
At first I mistakenly thought it was a background problem, but after debugging it was not the case, and no error was reported, and the success function was entered.
The following events and request parameters seem to be fine.
<a href="#" class="easyui-linkbutton" onclick="downLoadExcelTemplate()">下载模板</a> //下载模板 function downLoadExcelTemplate() { $.ajax({ type: 'POST', url: "DownLoadExcelTemplate", data: null, success: function (redata) { } }); };
In fact, this is the situation. Downloading files cannot be done directly through Ajax. If you need to use Ajax operation, we can write like this
After the request is successful, we create a < ;a> tag and then add the href attribute, and finally trigger the attribute and it will be OK
function downLoadExcelTemplate() { $.ajax({ type: 'POST', url: "DownLoadExcelTemplate", data: null, success: function (redata) { var $a = $("<a"); $a.attr("href", "DownLoadExcelTemplate"); $("body").append($a); $a[0].click(); $a.remove(); } }); };
Related recommendations:
How to dynamically load JS and CSS files and code scripts in native JS
How to implement the method of uploading and compressing images in js
The above is the detailed content of How to request download of Execl file via Ajax. For more information, please follow other related articles on the PHP Chinese website!