2014-08-27 4 views
0

Eclipse MVC 3의 Ajax 호출 컨트롤러에 Eclipse 케플러 IDE를 사용하는 데 문제가 있습니다. 나는 유튜브에 튜토리얼을 찾고 공부를위한 프로젝트를 만든다. 컨트롤러에 Ajax 호출 할 때 이전 문제가 너무 있지만 시간이 지나면 정상적으로 작동합니다. 내 프로젝트는 Spring MVC 프로젝트에서 만들어졌고, 나는 Ajax 호출로 콘트롤러를 치지 않고 모든 요청 jar를 가지고있다. thise 내 conf의 파일, 제어 방법 및 JQuery와 아약스 호출ajax 사용시 컨트롤러에서 응답을 얻지 못함

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> 

서블릿-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; 
} 

과 같은 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 요소는 <script> 태그에 JSP 페이지에 있습니다 아약스 호출

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

은 다른 사람이에 respong하지 않는 것이 Ajax 호출에 문제가 않습니다 이클립스 봄 mvc를 사용하여, 그게 바로 url 주소를 타격이 나던 아약스 메서드를 호출 할 때.

+1

은 브라우저를 사용하여 시도하고 검사하기 위해 불을 지르고를 사용 요청 –

+0

네트워크 콘솔에 무엇이 표시됩니까? –

답변

0

URL에서 ${pageContext.request.contextPath}를 제거 ${pageContext.request.contextPath}/getJSON/' + first + "/" + last

그것은, instaed하는 JS 파일에서 작동 앱 컨텍스트 직접 대체하지 않습니다 :
your-app/getJSON/' + first + "/" + last 또는 /getJSON/' + first + "/" + last

+0

var contact = "id :"+ result.id + "| name :"+ result.firstName + ""result.lastName + "| age :"+ result.age; -이 코드 줄에는 문제가 있었고, 결과를 잊어 버렸습니다 .lastName hehe. 도와 주신 모든 사람 – user2784713

관련 문제