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
响应没任何数据
求大神帮忙~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

解决方案 »

  1.   

    url:"/ajax",//改成这样试试
    type:"POST",你这个倒好,没有配置文根( content path)
      

  2.   

    谢谢您!!!!!!这边改成/ajax的话,结果是直接请求都没有发出去
      

  3.   


    方式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
      

  4.   

    恩恩,我之前用的是第二种方式,我配置的
    <Context path="" docBase="D:\programming\workspace\JQueryTest\WebContent"/>
    端口为80
    所以我在地址栏输入http://localhost/ajax,服务器会给我返回hello null 这个数据是正确的!
    但是放在$.ajax的url上,就没有任何数据返回,一直不知道是为什么呢?
    这边$.ajax的url可以配置为Servlet的名字,或者Action的名字,而不用加上主机名,那个是怎么样配置的呢?
      

  5.   

    1.检查jquery.js是否正确引入(路径对不对)
    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",
      

  6.   

    您好,我的问题已经解决了!!!也特别感谢您!!!!原因处在我将index.html放在了浏览器上面运行,那样没效果,而是应该在地址栏上输入http://localhost/index.html,这样就可以url直接Action的名字,也可以
    因为访问之后,再ajax请求会帮我们自动加上应用的根路径
      

  7.   


    好了就行,我没想到你直接运行的index.html没有通过url访问。