我试图在单击按钮时发送ajax post请求,我的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安全文件是
<?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");
}
我收到警报,但在我收到错误代码后,
我的ProfileBean类是,
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条答案
按热度按时间py49o6xq1#
这是你的
form
那是在你的addProfile()
呼叫你的按钮是
没有
type=
默认值是type=submit
因此,您的按钮会使表单提交到表单的url,该url(未设置)将与页面的url相同,而正是该(页面)url给出了405。最简单的解决方法是将按钮更改为
这样它就不会提交表单了。
其他选择:
你可以
将onclick更改为
onclick="addProfile();return false;"
或onclick="return addProfile();
和return false
从addprofile()(在$.ajax({});
)或(首选)
使用jquery绑定事件,并从提交尝试返回false