【问题标题】:How to send post ajax request from (.js) file to Spring MVC Controller?如何将来自(.js)文件的post ajax请求发送到Spring MVC控制器?
【发布时间】:2018-05-30 06:46:15
【问题描述】:

(.js)

$.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 控制器

@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");
}

通过运行此程序,我收到 404 错误。我只想使用 POST 发送请求。

【问题讨论】:

标签: ajax spring post model-view-controller controller


【解决方案1】:
$("#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);

    });
});

试试这个希望它工作正常

【讨论】:

  • $.post("localhost:8081/Mining_22_11_17/admin/getStateNames", { region : re , stId : state_id }, function(data) { alert("success"); //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); });但是还是报404错误
  • ${pageContext.request.contextPath} 添加这个因为它是 y 因为它采用项目路径默认
【解决方案2】:
  $.ajax({
    type: "POST",
    url: "/getStateNames",
    data: { region: re, stId: state_id },
    success : function(response) {
              alert(JSON.stringify(resoinse));
            },
            error: function(){                      
                alert('Error while request..');
            }
});

这应该可以,告诉我这是否解决了您的问题

【讨论】:

    猜你喜欢
    • 2021-08-09
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 2016-11-28
    • 2019-12-24
    • 1970-01-01
    相关资源
    最近更新 更多