代码之家  ›  专栏  ›  技术社区  ›  elcool codeVerine

window.location和window.open问题

  •  -1
  • elcool codeVerine  · 技术社区  · 14 年前

    我似乎解决不了这个问题。

    最初,JSP代码有一个JavaScript函数,可以从另一个服务器调用JSP:

    window.open("<%= otherServer %>/ourreports/Company/fooreport.jsp?index"+index,"Foo",options);
    

    在哪里? otherServer 是本地服务器(http://192.168.4.40:8080) 这样做很好,可以用fooreport.jsp弹出一个新窗口。

    现在的任务是指向同一服务器中的JSP。 所以,我把它改成

    window.open("/reports/Company/fooreport.jsp?index"+index,"Foo", options);
    

    我会得到一个 下载文件 弹出而不是页面

    我还尝试了以下所有操作:

    window.location = "/reports/Company/fooreport.jsp?index="+index;
    window.location.href = "/reports/Company/fooreport.jsp?index="+index;
    window.location = "http://localhost:9080/reports/Company/fooreport.jsp?index="+index;
    window.location.href = "http://localhost:9080/reports/Company/fooreport.jsp?index="+index;
    

    我仍然会弹出窗口将fooreport.jsp下载到我的电脑上。

    JSP格式良好,具有doctype、标记和<%@页声明…它本质上与之前调用的JSP相同

    我使用WebSphere 7.5.4,Java是1.5

    2 回复  |  直到 14 年前
        1
  •  0
  •   Community Mike Causer    7 年前

    window.location 如果 Content-Disposition 响应的标题已设置为 Attachment .

    response.setHeader("Content-Disposition", "attachment; filename=yourfile.ext");
    

    需要注意的是,在JSP中这样做是一个坏主意。如果响应涉及二进制数据,JSP可能会损坏它。在servlet中完成这项工作。JSP是用来编写模板文本的, not 编写Java代码。

        2
  •  0
  •   elcool codeVerine    14 年前

    这个问题存在于JSP中。 对于其他JSP,使用window.location和window.open的调用工作正常。

    问题出在“<%page”声明中。 我删除了它们,所以记不清了,但这与内容类型和ISO设置有关。 我把它们都拿出来了,只留下了“<%page import”声明,现在它可以正常工作了。