使用jQuery发送使用AJAX的Excel数据 [英] Use jQuery to send Excel data using AJAX
问题描述
我有以下的功能是从数据库中提取数据。 Ajax调用工作正常。我如何发送制表符分隔的数据,我的成功的功能给用户?设置contect类型为application / vnd.ms-EXCEL没有工作。成功警报显示格式正确的数据。
函数SendToExcel(){
$阿贾克斯({
键入:GET,
网址:/Search.aspx
的contentType:应用程序/ vnd.ms-EXCEL,
数据类型:文本,
数据:{编号:'+ASDF+'},
成功:功能(数据){
警报(数据);
},
错误:函数(jqXHR,textStatus,errorThrown){
警报(jqXHR.responseText);
}});
}
我不希望显示在浏览器中的数据 - 我想将它发送到Excel
编辑:我找到了一种方法做我想要的。相反,重定向用户到一个新的页面,该页面将提示他们保存/打开一个Excel文件,我打开的页面隐藏的iframe中。这样一来,用户单击一个按钮,系统会提示他们保存/打开一个Excel文件。没有页面重定向。难道是AJAX?没有,但它解决了实际问题,我有。
下面的功能我打电话按钮点击:
函数SendToExcel(){
VAR dataString =类型=练成'+
'和;数='+ $('#txtNumber)VAL()+
'和;参考='+ $('#txtReference)VAL()
$(#sltCTPick选项)。每个(函数(一){
dataString = dataString +'和;列='+ THIS.VALUE;
});
top.iExcelHelper.location.href =/Reports/JobSearchResults.aspx? + dataString ;;
}
AJAX是...是错误的选择。将用户重定向到要发送的数据来与正确的MIME类型的服务器资源,并让浏览器弄明白该怎么办。
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 user? Setting the contect type to "application/vnd.ms-excel" didn't work. The alert on success shows the correctly formatted data.
function SendToExcel() {
$.ajax({
type: "GET",
url: "/Search.aspx",
contentType: "application/vnd.ms-excel",
dataType: "text",
data: "{id: '" + "asdf" + "'}",
success: function(data) {
alert(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
}});
}
I don't want to display the data in the browser--I want to send it to Excel.
EDIT: I found a way to do what I wanted. Instead of redirecting the users to a new page that would prompt them to save/open an Excel file, I opened the page inside a hidden iframe. That way, the users click a button, and they are prompted to save/open an Excel file. No page redirection. Is it Ajax? No, but it solves the real problem I had.
Here's the function I'm calling on the button click:
function SendToExcel() {
var dataString = 'type=excel' +
'&Number=' + $('#txtNumber').val() +
'&Reference=' + $('#txtReference').val()
$("#sltCTPick option").each(function (i) {
dataString = dataString + '&Columns=' + this.value;
});
top.iExcelHelper.location.href = "/Reports/JobSearchResults.aspx?" + dataString;;
}
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.
这篇关于使用jQuery发送使用AJAX的Excel数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!