无法将ajax post请求发送到spring mvc控制器

时间:2021-05-20 11:44:10

标签: jquery ajax spring-mvc jsp spring-security

我正在尝试在单击按钮时发送 ajax 发布请求, 我的jsp表单是

 <body>
 <form name="userForm" id="userForm"  method="POST">
  <table><tbody id="uniform">
  <tr>
  </tr>
  <tr>
    <td>Name</td>
    <td><input name="name" type="text" /></td>
  </tr>
  <tr>
    <td>Rashi</td>
    <td><input name="rashi" type="text" /></td>
  </tr>
   <tr>
    <td>Gothram</td>
    <td><input name="gothram" type="text" /></td>
  </tr>
   <tr>
    <td>Gender</td>
    <td><input name="gender" type="text" /></td>
  </tr>
  <tr>
    
    <td><button class="btn btn-default" onclick="addProfile()">Add</button></td>
  </tr>
  </tbody>
  </table>
  </form></body>

js 中的函数,即 addProfile() 是,

    function addProfile(){
    alert("inside profile");
    $.ajax({
    url: "saveUser",
    type: "POST",
    data:  new FormData(this), // Data sent to server
    contentType: false, // The content type used when sending data to the server.
    cache: false, // To unable request pages to be cached
    processData:false,  // To send DOMDocument or non processed data file it is set to false
    success: function(data){
        alert(${profile_id});
            if(data=="registered"){}
            
    },
    error: function(){}             
     });
     }

我的控制器代码是,

@RequestMapping(value="/saveUser", method = RequestMethod.POST)
@ResponseBody
public void insert(ModelMap model, Principal principal,HttpSession session,@ModelAttribute @Valid Profile profile,
         BindingResult result) throws IOException {
    System.out.println(" inside insert  ");
    
   System.out.println("profilec name"+profile.getName());
   System.out.println(result.getErrorCount());
   int profile_id=service.Insert(profile);
    model.addAttribute("profile_id",profile_id);
   }

spring-security 文件是

  <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:security="http://www.springframework.org/schema/security"
   xmlns:p="http://www.springframework.org/schema/p" 
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/security
                       http://www.springframework.org/schema/security/spring-security-3.1.xsd">

   <security:global-method-security secured-annotations="enabled" />
    <security:http auto-config="true" >
    <security:intercept-url pattern="/index*" access="IS_AUTHENTICATED_ANONYMOUSLY" /> 
    <security:access-denied-handler error-page="/login"/>
    
    <security:form-login login-page="/login" default-target-url="/index"
        authentication-failure-url="/fail2login" 
        username-parameter="username"
        password-parameter="password" />
    <security:logout logout-success-url="/logout" />

   </security:http>

    <security:authentication-manager>
    <security:authentication-provider>
  <!-- <security:password-encoder ref="encoder" /> -->
    <security:jdbc-user-service data-source-ref="dataSource"
            users-by-username-query=
                "select username,password, enabled from USER_MASTER where username=?"
            authorities-by-username-query=
                "select username,USER_ROLE from USER_ROLE where username =?  " />
                            
    </security:authentication-provider>
  
    </security:authentication-manager>

    <bean id="encoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
    <constructor-arg name="strength" value="10" />
    </bean>
    </beans>

即使我删除了 ajax 代码,我也收到了 405 错误,就像这样

function addProfile(){
    alert("inside profile");
   
     }

我收到警报,但在收到错误代码后,

enter image description here

我的配置文件 bean 类是,

public class Profile {

private String name;


private String rashi;


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getRashi() {
    return rashi;
}

public void setRashi(String rashi) {
    this.rashi = rashi;
}

public String getGothram() {
    return gothram;
}

public void setGothram(String gothram) {
    this.gothram = gothram;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

private String gothram;

private String gender;
 }

不允许 405 方法的原因是什么?

1 个答案:

答案 0 :(得分:1)

在您的 form 调用后发布的是您的 addProfile()

你的按钮是

<button class="btn btn-default" onclick="addProfile()">

如果没有 type=,默认值为 type=submit,因此您的按钮会导致表单提交到表单的 URL,如果未设置,它将与页面的 URL 相同,并且是 this(页面)提供 405 的网址。

最简单的解决方案是将您的按钮更改为

<button type='button' class="btn btn-default" onclick="addProfile()">

这样它就不会提交表单。


其他选项:

你可以

  • 将 onclick 更改为 onclick="addProfile();return false;"

  • onclick="return addProfile(); and return false 来自 addProfile()(在 $.ajax({}); 之后)

或(首选)

  • 使用 jquery 绑定您的事件并从提交尝试中返回 false
相关问题