我試圖通過JQuery AJAX向我的GAE應用發送POST請求,但是我沒有收到響應數據。我有一個非常簡單的servlet,它簡單地回顯我傳入的「msg」。還覆蓋了doOptions。當通過JQuery向Google App Engine發送POST請求時沒有結果
@Override
protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setHeader("Access-Control-Allow-Origin", "*");
resp.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
resp.setHeader("Access-Control-Max-Age", "1728000");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/javascript");
resp.setCharacterEncoding("utf-8");
String msg = req.getParameter("msg");
resp.getWriter().print(msg);
}
這是我如何通過JQuery的AJAX
var parameters = {msg:"hello"};
$.ajax({
type: 'POST',
url: service_url,
data: parameters,
success: successhandler,
error: errorhandler
})
稱爲如果我看我的交互是通過螢火,我看到這一點。
首先,JQuery的發出一個OPTIONS請求
Host lessondesigner.appspot.com
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Origin lessondesigner.appspot.com
Access-Control-Request-Me... POST
Access-Control-Allow-Orig... *
Access-Control-Allow-Meth... GET, POST, OPTIONS
Access-Control-Max-Age 1728000
Date Mon, 06 Sep 2010 01:11:56 GMT
Content-Type text/html
Server Google Frontend
Content-Length 0
下發出POST請求
Host lessondesigner.appspot.com
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept application/json, text/javascript, */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Content-Type application/x-www-form-urlencoded; charset=UTF-8
Referer lessondesigner.appspot.com/testAjax.html
Content-Length 21
Origin lessondesigner.appspot.com
Pragma no-cache
Cache-Control no-cache
Content-Type text/javascript; charset=utf-8
Content-Encoding gzip
Date Mon, 06 Sep 2010 01:11:56 GMT
Server Google Frontend
Cache-Control private, x-gzip-ok=""
Content-Length 25
然而,我得不到任何回報的響應!如果我使用Curl,它工作正常。
curl -d "msg=hello" lessondesigner.appspot.com/lessondesigner
我回來
"hello"
有誰知道這是爲什麼?另外,爲什麼JQuery首先執行OPTIONS請求?它甚至不跨域。
您可以確認POST請求中的內容數據包含編碼的味精參數和正確的數據嗎?我沒有看到編碼內容,但由於內容長度是21個字節,我假設實際內容在請求中,但從POST中忽略(可能因爲沒有顯示Firebug)。 – 2010-09-06 02:59:52
我還會將日誌記錄添加到doPost()以確認doPost正在被調用,無論它是否使用「msg」參數或「msg」作爲空字符串調用。 – 2010-09-06 03:00:15
我有日誌記錄來確認doPost正在正確執行並且數據正在返回。我認爲Curl的請求證明了這一點。 – Wan 2010-09-06 17:14:44