Issue
I am trying to send the ajax post request on button click, My jsp form is
<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>
the function in js i.e.,addProfile() is,
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(){}
});
}
My controller code is,
@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 file is
<?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>
And I am getting that 405 error even i remove that ajax code,like this
function addProfile(){
alert("inside profile");
}
I am getting alert,but then after I am getting the error code,
My profile bean class is,
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;
}
what could be the reason for 405-method not allowed?
Solution
It's your form
that is POSTing after your addProfile()
call.
Your button is
<button class="btn btn-default" onclick="addProfile()">
without type=
the default is type=submit
so your button causes the form to submit to the form's URL, which, not set, will be the same as page's url and it's this (page) url that's giving the 405.
The simplest solution is to change your button to
<button type='button' class="btn btn-default" onclick="addProfile()">
so that it doesn't submit the form.
Other Options:
You could
- change onclick to
onclick="addProfile();return false;"
or
onclick="return addProfile();
andreturn false
from addProfile() (after the$.ajax({});
)
or (preferred)
- use jquery to bind your events and return false from the submit attempt
Answered By - freedomn-m