JAX-RS Web服务通过浏览器,但不通过XMLHTTPRequest的
问题描述:
工作我写的JAX-RS服务下载的文件与Apache服务器:JAX-RS Web服务通过浏览器,但不通过XMLHTTPRequest的
服务的主要内容是 -
@GET
@Produces("application/pdf")
public Response convertCtoF() {
String path = "D:\\a.pdf";
File file=new File(path);
ResponseBuilder rb = Response.ok((Object) file);
rb.header("Content-Disposition","attachment; filename=a.pdf");
return rb.build();
}
这是工作当我通过网络浏览器访问它时。
但是,当我通过XMLHTTPRequest访问它不起作用。它给XMLHttp.Status = 0
客户端代码:
<!DOCTYPE html>
<html>
<head>
<script>
function Download()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open('GET','http://localhost:8080/RESTExample/ABC/ctofservice',true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
alert('xmlhttp.readyState == 4');
if (xmlhttp.status == 200) {
alert('xmlhttp.status == 200');
}
else
{
alert(xmlhttp.status);
}
}
}
}
</script>
</head>
<body>
<h2>File Downloading Web Interface</h2>
<div id="myDiv"></div>
<button type="button" onclick="Download()">Download File</button>
</body>
</html>
答
因为你下载文件,并出于安全原因,你不能这样做与阿贾克斯它不使用XMLHttpRequest工作。有各种解决方法可供尝试。我不会在这里复制这些内容,因此请尝试在Google上搜索“ajax文件下载”,然后选择您偏好的方法。