通过JQuery向Google App Engine发送POST请求时没有结果
我正试图通过jQueryAjax向我的GAE应用程序发送POST请求,但没有收到任何响应数据。我有一个非常简单的servlet,它只响应我传入的“msg”。也是最重要的选项通过JQuery向Google App Engine发送POST请求时没有结果,jquery,google-app-engine,Jquery,Google App Engine,我正试图通过jQueryAjax向我的GAE应用程序发送POST请求,但没有收到任何响应数据。我有一个非常简单的servlet,它只响应我传入的“msg”。也是最重要的选项 @Override protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setHeader("Access-Control-
@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);
}
下面是我如何通过jQueryAjax调用的
var parameters = {msg:"hello"};
$.ajax({
type: 'POST',
url: service_url,
data: parameters,
success: successhandler,
error: errorhandler
})
如果我通过FireBug查看我的交互,我会看到这一点
首先,JQuery发送一个选项请求
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首先执行选项请求?它甚至都不是跨域的。请尝试编写程序。这是一个跨来源的资源共享问题。这是一篇关于它的好文章 基本上,如果HTTPRequest头包含一个“origin”字段,则服务器必须用“Access Control Allow origin”响应进行回复。在我的例子中,我在选项请求中这样做,但在POST请求中没有这样做。因此firefox没有返回我的内容数据。要解决它,我必须这样做
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setHeader("Access-Control-Allow-Origin", "*");
resp.setContentType("text/javascript");
resp.setCharacterEncoding("utf-8");
String msg = req.getParameter("msg");
resp.getWriter().print(msg);
}
您能否确认POST请求中的内容数据包括编码的msg参数和正确的数据?我看不到编码的内容,但由于内容长度为21字节,我假设实际内容在请求中,但在POST中被忽略(可能是Firebug,因为没有显示)。我还将日志添加到doPost()以确认调用了doPost,无论是使用无
msg
参数调用它,还是使用msg
作为空字符串调用它。我有日志记录来确认doPost是否正确执行,数据是否返回。我认为Curl的请求有效的事实证明了这一点。我想这可能是因为firefox出于安全考虑以某种方式过滤掉了数据内容吗?我在Chrome上也得到了同样的东西