Spring Web Application在PUT调用上返回405错误

时间:2019-03-28 16:56:56

标签: javascript java ajax spring-mvc

我有一个Java Spring Web应用程序,该应用程序创建可以分配给用户的角色列表。但是,我在创建新角色时遇到问题,该新角色通过AJAX PUT调用返回405错误。该应用程序在Java 8和Spring 5.1.1上运行。

我尝试调试前端和后端。我发现,呼叫成功到达了后端,处理了呼叫并返回。但是,前端将声明发生了错误并返回405错误。但问题是,该错误并未提供有关确切失败原因的任何详细信息。我能找到的最多信息是以下消息:

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
    at Function.invokeGetter (<anonymous>:2:14)
    at Object.error (http://localhost:8000/xxx/admin-user-search.html:1011:10)
    at fire (http://localhost:8000/xxxx/webjars/jquery/3.1.1/jquery.js:3305:31)
    at Object.fireWith [as rejectWith] (http://localhost:8000/xxxx/webjars/jquery/3.1.1/jquery.js:3435:7)
    at done (http://localhost:8000/xxxx/webjars/jquery/3.1.1/jquery.js:9244:14)
    at XMLHttpRequest.<anonymous> (http://localhost:8000/xxxx/webjars/jquery/3.1.1/jquery.js:9484:9)

Javascript:

function submitCreateNewRole(){
    isBlank = false;  
    var myData;

     newRoleName = $('#modalUserRoleSearchText').val();
     newRoleDescription = $('#modelUserRoleDescText').val();

     if (newRoleName=='' || newRoleDescription==''){
         isBlank = true;
     }

     if (isBlank){
         appAPI.setErrorBannerRole("Blank data is not allowed. Please enter non-blank data to create new Role.");
     } else {
         var UserRoleSearchModel = {}; 
         var userRoleAction = "createNewUserRole" ;

         RoleModel.ldapName = newRoleName; 
         RoleModel.roleDesc = newRoleDescription;      
         var token = $("meta[name='_csrf']").attr("content");
         var URL = "json/admin-user-search?userRoleAction=" + userRoleAction + "&roleName=" + newRoleName + "&roleDesc=" + newRoleDescription;
         var req = JSON.stringify(RoleModel);
         var jqxhr = $.ajax({               
             type: "PUT",
             url: URL,
             headers: { "X-CSRF-TOKEN" : token },
             data: req,
             contentType: "application/json",
             error: function (xhr, status, error) {
                 console.log("Failure caught");
                 console.log(xhr.responseText);
             },
             success: function(data){
                 myData = data;
             }
         }).done(function( msg ) {
             $('#alertMessageSuccess').val('Successfully create new row');
          }).fail(function(jqxhr) {
          $('#alertMessageError').val('failed to create role' + newRoleName);   
          });
      }  

      return myData; 
 }

Java Spring:

 @RequestMapping(value = {
      "admin-user-search"
  }, method = RequestMethod.PUT)
  public ModelAndView createNewUserRole(@AuthenticationPrincipal Principal principal,
      @RequestParam(required = false) String pageCommand,
      @ModelAttribute("UserModel") UserModel userSearch,
      @ModelAttribute("RoleModel") RoleModel userRoleSearch,
      @RequestParam(value = "roleName", required = false) String roleName,
      @RequestParam(value = "roleDesc", required = false) String roleDesc,
      @RequestParam(value = "userRoleAction", required = false) String userRoleCommmand, HttpServletRequest request) {

    Results results = null;
    List<Role> roleVOs = null;
    String roleResponseMessage;

    ModelAndView rView = new ModelAndView("admin-user-search");

    if ("createNewUserRole".equals(userRoleCommmand)) {
      userRoleSearch.clearAlertMessages();
      userSearch.clearAlertMessage();

      if ("".equals(roleName)) {
        roleResponseMessage = "Unable to create a new role due to invalid or blank LDAP username enterred. Please try again with valid LDAP username.";
        userRoleSearch.setErrorMessages(roleResponseMessage);
      } else if ("".equals(roleDesc)) {
        roleResponseMessage = "Unable to create a new role due to invalid or blank Role Description entered.";
        userRoleSearch.setErrorMessages(roleResponseMessage);
      } else {
        try {
          this.tdmcRoleDao.addNewRole(roleName, roleDesc);
          roleResponseMessage = String.format("New user role '%s' has been added.", userRoleSearch.getLdapDn());
          userRoleSearch.setSuccessMessages(roleResponseMessage);
          userSearch.setSuccessMessages(roleResponseMessage);

          roleVOs = retrieveAllRoles();
        } catch (final SQLException e) {
          LOGGER.error(e, TDMCMessages.TDMC_0142_DATABASE_INSERT_EXCEPTION, "tdmcRoleDao.addNewRole(newRoleLdap)");
          roleResponseMessage = "Unable to create a new role -'%s' due to DB problem. Please retry with a new  valid role name.";
          userRoleSearch.setErrorMessages(roleResponseMessage);
          userSearch.setErrorMessages(roleResponseMessage);
        } catch (final DuplicateKeyException dupEx) {
          roleResponseMessage = "Unable to create a duplicate role'. Please retry with non-duplicated role name.";
          userRoleSearch.setErrorMessages(roleResponseMessage);
          userSearch.setErrorMessages(roleResponseMessage);
        }

        if (roleVOs != null && !roleVOs.isEmpty()) {
            results = populateRolesToResults(roleVOs);
        }

        userRoleSearch.setResults(results);
        userRoleSearch.setSelected(roleVOs);

        rView.addObject("RoleModel", userRoleSearch);
      }
    }

    return rView;
}

当我运行应用程序并尝试创建新角色时,我看到PUT调用到达Java服务器并成功返回了视图。但是,在Web客户端,它会抛出405错误,并且不清楚到底是什么原因导致的。任何见解将非常有帮助。

另一方面,该应用程序还可以进行POST和GET调用,但是它们似乎可以正常工作,因此我无法理解为什么在这种情况下PUT调用失败了。

编辑:修复代码

1 个答案:

答案 0 :(得分:0)

首先,您的网址似乎有误,请检查。 然后更改为发布贴图,然后通过正文进行发布 就像@requesrbody