2013-02-20 104 views
0

我正在尝试使用Jquery Ajax对我的Spring MVC应用程序进行Ajax调用。应用程序控制器工作正常,但我无法让这个Ajax测试控制器工作。该警报被触发,但是对控制器的调用从不做出。我也尝试过使用load,get,post。他们都没有打电话给服务器。这让我觉得我正在做一些明显错误的事情。如果我将URL直接放在浏览器地址栏上,则会调用控制器。Spring MVC + JQuery + Ajax问题

如果有人能指导我正确的方向或告诉我我做错了什么,我会很感激。

的JavaScript

<html> 
<head> 
<script type="text/javascript" src="jquery-1.8.1.min.js"> </script> 
<script type="text/javascript"> 
    function doAjax() { 
     alert("did it even get here?"); 
     $.ajax({ 
      url : "simpleRequestTest.do", 
      method: "GET",   
      success : function(response) { 
       $('#show').html(response); 
      } 
     }); 
    } 
</script> 
</head> 
<body> 
    <h1>Simple Test </h1> 
    <a href="javascript:doAjax();"> Simple Test </a> 
    <br /> 
    <div id="show">...</div> 
</body> 
</html> 

控制器

@RequestMapping("/simpleRequestTest") 
public @ResponseBody String performSimple() { 
    return "Very Simple Test"; 
} 
+3

提示:使用开发者工具在浏览器中观看网络流量。你可以看到究竟哪个URL被打了,服务器返回了什么。这很可能会为您提供所需的信息。另外,你是否试过简单地将浏览器指向simpleRequestTest.do?怎么了? – StriplingWarrior 2013-02-20 05:25:15

+0

你正在请求'simpleRequestTest.do',但你的控制器映射是'/ simpleRequestTest'没有'.do'。我认为服务器扔404未找到错误 – 2013-02-20 05:33:03

+0

我认为你的ajax网址是不正确的。 – Jason 2013-02-20 05:44:45

回答

1

你可以检查你是否使用正确的路径来包含jquery-1.8.1.min.js。

我检查了它的工作文件代码为我。

1

我认为你是在ajax通话

失踪dataType试试这个

function doAjax() { 
     alert("did it even get here?"); 
     $.ajax({ 
      url : "simpleRequestTest.do", 
      method: "GET",   
      success : function(response) { 
       $('#show').html(response); 
      }, 
      dataType: 'text' 
     }); 
    } 
1

只要改变url : "simpleRequestTest.do",url : "simpleRequestTest",method: "GET",到​​

我认为方法中的jQuery 1.5版被删除,最新