2017-12-16 13 views
0

(.jsファイル)(.js)ファイルからポストajaxリクエストをSpring MVC Controllerに送信するにはどうすればいいですか?私は404 error.Iを取得しています。このプログラムを実行することにより

@RequestMapping(value="/getStateNames",method = RequestMethod.POST) 
    public @ResponseBody RegionDistrict add(HttpServletRequest request, HttpServletResponse response,@RequestParam String region, @RequestParam String stId) { 
     System.out.println("Get state"); 
} 

$.ajax({ 
       type: "POST", 
       //contentType : "application/json", 
       dataType : "json", 
       url: "getStateNames", 
       //url:"http://localhost:8081/Mining_22_11_17/pages/admin/a.jsp", 
       cache: false,    
       data: "region=" + re + "& stId=" + state_id, 
       success: function(response){ 
        //$('#result').html(""); 
        alert("hiiii state list"); 
        var obj = JSON.parse(response); 
        alert("state list" + obj); 
        //$('#result').html("First Name:- " + obj.firstName +"</br>Last Name:- " + obj.lastName + "</br>Email:- " + obj.email); 
       }, 
       error: function(){      
        alert('Error while request..'); 
       } 
      }); 

Spring MVCのコントローラはPOSTを使用して要求を送信します。 enter image description here

+0

再と状態はスクリプトで – jeevanswamy21

+0

httpsを宣言STATE_ID解決するかどうか私に教えてください:// WWWを。 – jeevanswamy21

答えて

0
$("#yourID").click(function(event) { 

    var region = $('#id').val(); 
    var state_id = $('#edited').val(); 


    $.post("${pageContext.request.contextPath}/getStateNames", { 
     region : region , 
     state_id : state_id 
    }, function(data) { 

     //var json = JSON.parse(data); 
     //... 

    }).done(function(data) { 
     alert("hiiii state list"); 
     swal("success"); 
     //location.reload(); 
    }).fail(function(xhr, textStatus, errorThrown) { 
    }).complete(function() { 
     //$("#btn-save").prop("disabled", false); 

    }); 
}); 

+0

$ .post( "http:// localhost:8081/Mining_22_11_17/admin/getStateNames"、{ \t、mkyong.com/spring-mvc/spring-mvc-refactoring-a-jquery-ajax-post-example/ 210領域:STID \t \t、RE: \t \tをSTATE_ID}、関数(データ){ \t \t \t \tアラート( "成功")。 \t \t // var json = JSON.parse(data); \t \t // ... \t \t})行わ(関数(データ){ \t \tアラート( "hiiii状態リスト")。 \t \t SwaIで( "成功"); \t \t //位置.reload();。。 \t \t})失敗(関数(XHR、textStatus、errorThrown){ \t \t})完全(関数(){ \t \t //$("#btn-save").prop (「無効」、偽); \t \t});しかし、まだ404エラーを与える –

+0

$ {pageContext.request.contextPath}これは、プロジェクトパスのデフォルトをとるため、yと同じように追加します – jeevanswamy21

0
$.ajax({ 
    type: "POST", 
    url: "/getStateNames", 
    data: { region: re, stId: state_id }, 
    success : function(response) { 
       alert(JSON.stringify(resoinse)); 
      }, 
      error: function(){      
       alert('Error while request..'); 
      } 
}); 

その作品は罰金この希望を試すこれは、仕事これはあなたの問題

関連する問題