使用Ajax下载文件在Firefox上不起作用

时间:2018-08-15 11:06:27

标签: c# ajax asp.net-mvc

我正在尝试使用Ajax下载生成的文件(pdf),它在Chrome和IE中完美运行,但在FireFox中却无法正常运行。 这是我的代码:

function Download(urlAction, urlDownload) {

    $.ajax({
        type: "post",
        url: urlAction,
        data: {
            'itemIds': checkedItems,
            'dateMin': datemin.toISOString(),
            'dateMax': datemax.toISOString()
        },
        datatype: "json",
        traditional: true,
        success: function (data) {
            console.log('fff', data);
            if (data.success) {
                window.location = urlDownload;
            }
        }
        error: function (xhr, textStatus, err) {
            console.log("readyState: " + xhr.readyState);
            console.log("responseText: " + xhr.responseText);
            console.log("status: " + xhr.status);
            console.log("text status: " + textStatus);
            console.log("error: " + err);
        }
    });
}
}

在UrlAction中,我以Json格式生成文件并将其发布到会话中,然后在我的urlDownload中再次调用它。 在Chrome和IE中,无需重新加载页面即可下载文件,但在Firefox中,仅重新加载页面。

错误提示:

  • readyState:0
  • responseText:未定义
  • 状态:0
  • 文本状态:错误
  • 错误:未定义

2 个答案:

答案 0 :(得分:0)

Jquery $.ajax说:

  

dataType(默认值:Intelligent Guess(xml,json,脚本或html))   类型:字符串您要从   服务器。如果未指定任何内容,则jQuery将尝试根据   响应的MIME类型

因此您可以尝试先删除该参数。

关于错误状态,为0,您可以在此处找到详细的原因: XMLHttpRequest status 0 (responseText is empty)

关于您的错误处理程序,error: function (xhr, textStatus, err)

  1. xhr.status显示了原因,您必须检查服务器端配置;但是您没有提到什么是服务器环境。
  2. textStatus可以是以下值:"timeout", "error", "abort", and "parsererror";
  3. 如果请求未到达您的服务器端位置,则
  4. err可能不确定;

这是官方文件:

  

类型:函数(jqXHR jqXHR,字符串textStatus,字符串errorThrown)A   如果请求失败,则调用该函数。功能接收   三个参数:jqXHR对象(在jQuery 1.4.x中,XMLHttpRequest),一个   描述发生的错误类型的字符串和可选的   异常对象(如果发生)。第二个可能的值   参数(除null外)是“超时”,“错误”,“中止”和   “ parsererror”。发生HTTP错误时,errorThrown会收到   HTTP状态的文本部分,例如“未找到”或“内部   服务器错误。”从jQuery 1.5开始,错误设置可以接受一个数组   功能。每个函数将依次调用。注意:此处理程序   跨域脚本和跨域JSONP请求未调用。   这是一个Ajax事件。

答案 1 :(得分:0)

对我有用的是,将按钮(调用此方法的按钮)从div窗体中移出,它停止了重新加载页面的工作,下载像是很吸引人的。

相关问题