【发布时间】:2021-08-09 16:00:32
【问题描述】:
我正在尝试在单击按钮时发送 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");
}
我收到警报,但在收到错误代码后,
我的个人资料 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 方法的原因是什么?
【问题讨论】:
-
你的页面刷新了吗?如果是,您的表单正在提交,则不会启动 ajax 调用。
-
即使我删除了 ajax 代码,我也会收到 405 错误 - 那么 (显然) 而不是导致 405 的 ajax POST。看在浏览器网络选项卡中仔细查找导致 405 的是的确切原因。
标签: jquery ajax spring-mvc jsp spring-security