2014-08-27 65 views
0

在使用eclipse kepler ide的Spring MVC 3中,Ajax调用控制器有问题。我在youtube上找到了一些tutoruials并为学习做了项目。当我有Ajax调用控制器时,我也遇到过问题,但过了一段时间才正常工作。我的项目是在Spring MVC项目中完成的,我已经请求了所有jar,只是没有通过Ajax调用来触发控制器。 thise是我的conf文件,控制方法和jQuery的AJAX调用在使用ajax的时候,Spring MVC中没有得到控制器的响应

的web.xml

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/root-context.xml</param-value> 
</context-param> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

的servlet-context.xml的

<mvc:resources location="/resources/" mapping="/resources/**"/> 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean>

<context:component-scan base-package="com.milan.springajax.controller" /> 

我控制器的方法

@RequestMapping(value="/getJSON/{firstName}/{lastName}", method=RequestMethod.GET) 
public @ResponseBody 
Contact findByName(@PathVariable("firstName") String first, @PathVariable("lastName") String last, 
     HttpServletRequest request, HttpServletResponse response){ 
    Contact contact=contactService.findByName(first, last); 
    return contact; 
} 

和Ajax调用谁坐落在JSP页面中<script>标签在同一个JSP页面

$(document).ready(function() { 
$('.button').on('click', function() { 
    var first=$('#firstInput').val(); 
    var last=$('#lastInput').val(); 
    alert("button je stisnut " + first + " " + last); 

    $.ajax({ 
     type:"GET", 
     url:'${pageContext.request.contextPath}/getJSON/' + first + "/" + last, 
     dataType:'json', 
     success: function(result) { 
      var contact = "id: " + result.id + 
       " | name : " + result.firstName + " " result.lastName + 
       " | age : " + result.age; 


      $('#theJson').html(contact); 

     }, 

     error: function(jqXHR, textStatus, errorThrown) { 
      alert("Contact " + textStatus + errorThrown + " !"); 
     } in thise line i get warning that i miss semicolon ??? 
}); 

}); 
}); 

和HTML元素

 <div> 
      <label for="firstInput">First Name</label> 
      <input type="text" name="firstName" id="firstInput"> 
     </div> 
     <div> 
      <label for="laststInput">Last Name</label> 
      <input type="text" name="lastName" id="lastInput"> 
     </div> 
     <div id="theJson"></div> 
     <button type="button" class="button" id="button">Fetch JSON</button> 

没有任何人与Ajax调用,在不respong问题eclipse使用spring mvc,它在调用thise ajax方法时不会触及正确的url地址。

+1

尝试用浏览器中使用,并使用Firebug检查请求 – 2014-08-27 16:31:41

+0

您的网络控制台显示什么? – 2014-08-27 16:40:57

回答

0

从URL删除${pageContext.request.contextPath}${pageContext.request.contextPath}/getJSON/' + first + "/" + last

它不会在一个JS文件时,instaed,直接替换为你的应用程序上下文:
your-app/getJSON/' + first + "/" + last/getJSON/' + first + "/" + last

+0

var contact =“id:”+ result.id + “| name:”+ result.firstName +“”result.lastName + “| age:”+ result.age; - 在这行代码是一个问题,我忘了+ result.lastName嘿。 thx所有的帮助 – user2784713 2014-08-28 09:38:50

相关问题