0
工作我寫的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>