2014-02-22 5 views
1

MVC 스프링 컨트롤러가 있습니다. 페이지로드시이 아약스를 호출합니다.ajax 호출 MVC 스프링 컨트롤러를 찾을 수 없음 오류

$.ajax({ 
      type:  "get", 
      url:  'custom/Service', 
      data:  "note1=" + "11", 
      dataType: "json", 
      async: false, 
      success: function() { 
         alert("1"); 
      }, 
      error:function (xhr, ajaxOptions, thrownError){ 
       if(xhr.status == 200 && xhr.statusText == 'parsererror'){ 
        window.location.reload(); 
       } else { 
        alert(xhr.status+","+xhr.statusText); 
        alert(thrownError); 
       } 
      }  
     }); 

내 컨트롤러는 다음과 같습니다

@RequestMapping("/custom") 
public class CustomController { 

    @RequestMapping(value="/Service", method=RequestMethod.GET) 
     public String Service(
       @RequestParam("note1") String note1, 
       HttpServletRequest request, HttpServletResponse response, Locale locale, 
       Model model) { 
      String result = custom.Service(note1, request, response); 
     System.out.println("result: " + result); 
     return result; 
    } 
} 

아웃 된 풋 컨트롤러의 콘솔에서 올바른 것입니다. 하지만 "찾을 수 없음"오류가 발생합니다. 다음은 개발자 도구 오류입니다. "MySite/custom/Service? note1 = 11"404 (찾을 수 없음)를 가져옵니다. 뭐가 잘못 되었 니?

답변

1
은 다음과 같이 코드를 변경

:

@RequestMapping(value="/Service", method=RequestMethod.GET) 
     public @ResponseBody String Service(
       @RequestParam("note1") String note1, 
       HttpServletRequest request, HttpServletResponse response, Locale locale, 
       Model model) { 
      String result = custom.Service(note1, request, response); 
     return result; 
    } 

당신은 당신의 아약스 성공 함수에서 결과에 액세스 할 수 있습니다.

success: function(result) { 
         alert(result); 
      }, 
+0

경로가 정확합니다. 콘솔에서 출력을 볼 수 있기 때문에. 나는 프로젝트에서 다른 아약스 호출에 대한 경로를 사용하고 작동합니다. – Mark

+0

결과가 틀림 없습니다. 맞습니까? jsp 이름이어야합니다. –

+0

난 그냥 클래스를 생성하고이 같은 점에서 모든 매핑을 넣어 : @RequestMapping을 ("/ 사용자 정의") 공용 클래스 CustomController {\t @RequestMapping (값 = "/ 서비스", 방법 = RequestMethod.GET) \t 공공 문자열 ServiceType 자리를 ( \t \t \t @RequestParam ("주") 문자열 주, \t \t \t HttpServletRequest의 요청 HttpServletResponse를 응답 로케일 로케일 \t \t \t 모델 모델) { \t \t 문자열 결과 = custom.Service (주 요청, 응답); \t System.out.println ("result :"+ result); \t 반품 결과; } – Mark

0

당신은 reposnsebody 주석, 수익 매개 변수 스프링 컨트롤러는 뷰를 반환 할 필요가 없습니다

public @ReponseBody String Service(

,이 데이터를 반환 할 수 있습니다에 응답 본문 주석을 추가해야합니다. 원래 코드는 뷰 해석기 설정에 따라 결과 변수를 JSP로 검색합니다.

관련 문제