index.js这边是这样的:
$(function(){
$.ajax({
url:"http://localhost/ajax",
data:"username=yinjian&password=123",
dataType:"json",
success:function()
{
alert(arguments);
}
});
});而servlet是这样的
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username = req.getParameter("username");
System.out.print("username = "+username);
PrintWriter out = resp.getWriter();
out.write("hello "+username);
}其他配置都没有问题:
如果在浏览器的地址栏输入http://localhost/ajax
返回
hello null但是如果我在index.html载入index.js的代码时,却没有响应的数据,这是怎么一会儿事呢??
用firbug的结果
响应头信息
Content-Length 13
Date Thu, 18 Oct 2012 01:38:47 GMT
Server Apache-Coyote/1.1
请求头信息
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Connection keep-alive
Host localhost
Origin null
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20100101 Firefox/14.0.1
响应没任何数据
求大神帮忙~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$(function(){
$.ajax({
url:"http://localhost/ajax",
data:"username=yinjian&password=123",
dataType:"json",
success:function()
{
alert(arguments);
}
});
});而servlet是这样的
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username = req.getParameter("username");
System.out.print("username = "+username);
PrintWriter out = resp.getWriter();
out.write("hello "+username);
}其他配置都没有问题:
如果在浏览器的地址栏输入http://localhost/ajax
返回
hello null但是如果我在index.html载入index.js的代码时,却没有响应的数据,这是怎么一会儿事呢??
用firbug的结果
响应头信息
Content-Length 13
Date Thu, 18 Oct 2012 01:38:47 GMT
Server Apache-Coyote/1.1
请求头信息
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Connection keep-alive
Host localhost
Origin null
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20100101 Firefox/14.0.1
响应没任何数据
求大神帮忙~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type:"POST",你这个倒好,没有配置文根( content path)
方式1 :直接将程序(如 test) 放到tomcat目录\webapps 目录下。
方式2 :配置\conf\server.xml 在<Host></Host>内
增加:
<Context path="/test" docBase="D:\test" debug="0"
reloadable="true" crossContext="true">
</Context>程序访问:http://localhost/test/ajax
<Context path="" docBase="D:\programming\workspace\JQueryTest\WebContent"/>
端口为80
所以我在地址栏输入http://localhost/ajax,服务器会给我返回hello null 这个数据是正确的!
但是放在$.ajax的url上,就没有任何数据返回,一直不知道是为什么呢?
这边$.ajax的url可以配置为Servlet的名字,或者Action的名字,而不用加上主机名,那个是怎么样配置的呢?
2.调整为:
$(function() {
$.ajax({
type: "POST",
url: "http://localhost/ajax",
data: "username=yinjian&password=123",
dataType: "json",
success: function(x) {
alert(x);
}
});
});3.不行的话加上文根试试
<Context path="/test" docBase="D:\programming\workspace\JQueryTest\WebContent"/>url: "/test/ajax",
因为访问之后,再ajax请求会帮我们自动加上应用的根路径
好了就行,我没想到你直接运行的index.html没有通过url访问。