step1: in spring mvc submitting form using ajax,worked fine.
step2: integrating same spring mvc project spring security(without ajax form submit in spring mvc) worked fine.
but when tried integrate same (step2 project) spring mvc + spring security , introducing ajax form submit spring mvc form end error 405 on browser
405: method not allowed, request method 'post' not supported
**general** request url:http://localhost:8080/springmvc-ajax-security/submitform.web request method:post status code:405 method not allowed remote address:[::1]:8080 **response header** allow:get cache-control:no-cache, no-store, max-age=0, must-revalidate content-length:1085 content-type:text/html date:sat, 26 mar 2016 12:12:00 gmt expires:0 pragma:no-cache server:apache-coyote/1.1 x-content-type-options:nosniff x-frame-options:deny x-xss-protection:1; mode=block **request header** accept:application/json, text/javascript, */*; q=0.01 accept-encoding:gzip, deflate accept-language:en-us,en;q=0.8 connection:keep-alive content-length:117 content-type:application/json; charset=utf-8 cookie:jsessionid=019622188db97def5f2d1ae716032c41 host:localhost:8080 origin:http://localhost:8080 referer:http://localhost:8080/springmvc-ajax-security/helloworld.web user-agent:mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/49.0.2623.87 safari/537.36 x-requested-with:xmlhttprequest **request payload** {"studentname":"noorus","studentbranch":"cs","studentdept":"computer","_csrf":"b68fbffe-d7a0-40eb-9edc-74d0f6408556"}
studentcontroller.java
@requestmapping(value="/submitform.web", method = requestmethod.post) public @responsebody student submittedfromdata(@requestbody student student, httpservletrequest request) { return student; }
student.jsp
<body> <form:form id="submitform" action="submitform.web" method="post" commandname="student"> <fieldset style="width: 300px;"> <legend>student details</legend> <ol> <li><label for=studentname>student name</label> <form:input path="studentname" name="studentname" type="text" placeholder="first , last name" /></li> <li> <p> <label for=studentbranch>student branch</label> <form:input path="studentbranch" name="studentbranch" type="text" /> </p> </li> <li> <p> <label for=studentdept>student department</label> <form:input path="studentdept" name="studentdept" type="text" required="true" /> </p> </li> </ol> </fieldset> <fieldset style="width: 300px;"> <input id="submitid" type="submit" value="submit form"> </fieldset> </form:form> </body> <!-- <script type="text/javascript" src="resources/js/submit.js"></script> --> <script type="text/javascript"> $(document).ready(function() { alert("welcome js page"); $('#submitform').submit(function(e) { var frm = $('#submitform'); e.preventdefault(); var data = {} var form = this; //gather data remove undefined keys(buttons) $.each(this, function(i, v){ var input = $(v); data[input.attr("name")] = input.val(); delete data["undefined"]; }); $.ajax({ contenttype : 'application/json; charset=utf-8', type: frm.attr('method'), url: frm.attr('action'), datatype : 'json', data : json.stringify(data), success : function(callback){ alert("response: name: "+callback.studentname+" branch: "+callback.studentbranch+" department: "+callback.studentdept); $(this).html("success!"); }, error : function(){ $(this).html("error!"); } }); }); }); </script>
student.java
@entity @table(name="student") public class student implements serializable{ private static final long serialversionuid = 1l; @id @generatedvalue(strategy=generationtype.auto) @column(name="studentid") long studentid; @column(name="studentname") string studentname; @column(name="studentdept") string studentdept; @column(name="studentbranch") string studentbranch; public student() { super(); } getter & setter }
thanks in advance
i had similar issue, using ajax spring security , in case disabling csrf worked security configuration.
@configuration public class securityconfiguration extends websecurityconfigureradapter { @override protected void configure(httpsecurity httpsecurity) throws exception { httpsecurity.csrf().disable(); } }
Comments
Post a Comment