【问题标题】:ajax call returns 404ajax 调用返回 404
【发布时间】:2023-04-01 05:05:01
【问题描述】:

我正在尝试将我的表单作为 JSON 数据提交给控制器以进行进一步处理。我正在使用 jquery 并使用以下代码提交数据:

我的js代码:

    $(document).ready(function(){
        alert('hi');
        $("#btn-save").click(function(event) {
               var data = {}
               alert($("#name").val());
            data["name"] = $("#name").val();
            data["address"] = $("#address").val();
            data["phone"] = $("#phone").val();
            data["zipCode"] = $("#zipCode").val();
            $("#btn-save").prop("disabled", true);
            alert('1'+JSON.stringify(data));
            $.ajax({
                 type: "POST",
                 contentType: "application/json",
                 url: "/saveUser",
                 data: JSON.stringify(data),
                 dataType: 'json',
                 timeout: 600000,
                 success: function (data) {
                    alert('success');
                 },
                 error: function(xhr, status, error){
                     var errorMessage = xhr.status + ': ' + xhr.statusText
                     alert('Error - ' + errorMessage);
                 }
        });
           });
    });
</script>

我已经编写了可以处理请求的控制器类。但执行永远不会到达这个方法。 这预计会调用控制器类:

@RequestMapping(value = "/saveUser", method = RequestMethod.POST)
public @ResponseBody SysUserDto  saveUser(@RequestBody SysUserDto user) {
    System.out.println("Save code******");
    return user;
}

我什至尝试不使用响应正文并将返回作为 void 而不是 SysUserDto 但是当执行进入错误块时,我看到 404 错误。请帮忙

【问题讨论】:

  • 控制器类是否有可能未被组件扫描?
  • 可以直接访问saveUser-Url吗(将RequestMethod改为GET后)?
  • 得到解决 [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported] 当我尝试访问 URL localhost:8010/OMS/saveUser

标签: jquery ajax spring spring-mvc


【解决方案1】:

你可以试试这个。假设您的表单 ID 称为“uploadForm”

 $(document).ready(function(){
            alert('hi');
            $("#btn-save").click(function(event) {
             event.preventDefault();
             var user= new FormData($('#uploadForm')[0]);        
                $.ajax({
                    contentType : false,
                    processData : false,
                     type: 'POST',
                     url: "/saveUser",
                     data: user,
                     timeout: 600000,
                     success: function (data) {
                        alert('success');
                     },
                     error: function(xhr, status, error){
                         var errorMessage = xhr.status + ': ' + xhr.statusText
                         alert('Error - ' + errorMessage);
                     }
            });
               });
        });
    </script>

控制器:

@RequestMapping(value = "/saveUser", method = RequestMethod.POST)
public @ResponseBody String saveUser(SysUserDto user) {
    System.out.println("Save code******");
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(user);
    System.out.println(json);
    return json;
}

ObjectMapper 来自杰克逊图书馆。

【讨论】:

    猜你喜欢
    • 2012-03-29
    • 2011-08-11
    • 2022-01-10
    • 2011-01-04
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    相关资源
    最近更新 更多