我正在使用JavaScript向servlet發送AJAX請求。該小服務程序確實在回覆,但響應標題爲空,響應文本也爲空。對AJAX請求的Servlet響應爲空
當我嘗試相同的客戶端代碼,而不是發送請求到PHP頁面,它工作正常。
這裏有兩個客戶端(你可以嘗試一下,看看他們的源):
- AJAX到的servlet:
http://79.136.61.27/web/ajax-to-servlet。 HTML - AJAX到PHP:
http://79.136.61.27/web/ajax-to-php.html
發送請求到servlet時的輸出是:
Response will go below
Response:
responseText was null!
Headers:
null response headers!
將請求發送到PHP時的輸出是:
Response will go below
Response:
Hi from php
Headers:
Date: Sun, 17 Apr 2011 11:58:57 GMT Server: Apache/2.2.17 (Win32) PHP/5.3.6 X-Powered-By: PHP/5.3.6 Content-Length: 18 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/html
這裏是servlet的代碼。正如您所看到的,我正在試驗一些設置標題和內容類型,但是我的任何實驗都沒有任何效果。奇怪的是,我做了一個hello world這樣的例子,就像最近使用servlets一樣,它工作得很好,沒有搞亂頭文件和東西。但現在它不再適用了。 :(
package simple;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = -6713061702557291351L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String out = "<p>Hi from servlet!</p>";
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
System.out.println("got request");
PrintWriter pw = response.getWriter();
pw.write(out);
pw.flush();
boolean error = pw.checkError();
System.out.println("Error? " + error);
}
}
hifromphp.php很簡單:
<?php
echo "<p>Hi from php</p>";
?>
感謝您的閱讀,並在此先感謝
編輯:我意識到,這些鏈接不會永遠工作所以,歸檔。目的我在此粘貼ajax-to-servlet.html。除了請求去的URL之外,ajax-to-php.html是相同的。 ajax-to-html.html:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Send ajax</title>
</head>
<body>
<script type="text/javascript">
/* <![CDATA[ */
function getXMLHttp() {
var xmlHttp = new XMLHttpRequest();
return xmlHttp;
}
function sendAjax() {
var xmlHttp = getXMLHttp();
var divResp = document.getElementById("response");
var divHdrs = document.getElementById("responseHeaders");
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
var hdrs = xmlHttp.getAllResponseHeaders();
var resp = xmlHttp.responseText;
divHdrs.innerHTML = "<p>Headers:</p><p>" + (hdrs ? hdrs : "null response headers!<p>");
divResp.innerHTML = "<p>Response:</p>" + (resp ? resp : "<p>responseText was null!<p>");
}
}
xmlHttp.open("GET", "http://79.136.61.27:8080/SimpleServlet/SimpleServlet", true);
xmlHttp.send(null);
}
/* ]]> */
</script>
<p><input type="button" value="Send Ajax" onclick="javascript: sendAjax();"/></p>
<p>Response will go below</p>
<div id="response"></div>
<div id="responseHeaders"></div>
</body>
</html>
你在運行你認爲正在運行的servlet類嗎? – BalusC 2011-04-17 12:25:23
感謝您的評論。是的,我可以看到它是如何調用的以及它如何響應代碼中的更改,所以我發佈的代碼確實在執行。 – 2011-04-17 12:31:21