javascript - Ajax success function not triggered even though the request to the API is made succesfully why? -


i don't know why success function not triggered error function executed though when make post request api happens planned.

this js:

$("form").submit(function (env) {     env.preventdefault();     $("#submitbtn").prop('disabled', true);     $("#form_result").text("");     var request = json.stringify($("#newrequest-form").serializeobject());     console.log(request);     $.ajax({         method: "post",         url: "/api/holidays",         data: request,         contenttype: "application/json",         datatype: "json",         success: function () {             $("#form_result").text("submitted succesfully");             $(this).prop('display', 'hidden');         },         error: function (error) {             $("#form_result").text("error: creating request");             $("#submitbtn").prop('disabled', false);         }     }); }); 

the form:

<div class="formcontainer">     <div class="container-fluid">         <div class="row">             <div class="col-md-6 col-sm-6 col-xs-12">                 <form class="form-horizontal" id="newrequest-form">                     <div class="form-group ">                         <label class="control-label " for="reason">                             reason                         </label>                         <textarea class="form-control" cols="40" id="reason" name="reason" rows="10"                                   required=""></textarea>                     </div>                     <div class="form-group ">                         <label class="control-label " for="holidaytype">                             holiday type                         </label>                         <select class="select form-control" id="holidaytype" name="holidaytype">                             <option value="medical">                                 medical                             </option>                             <option value="rest holiday">                                 rest holiday                             </option>                             <option value="other">                                 other                             </option>                         </select>                     </div>                     <div class="form-group ">                         <label class="control-label " for="startdate">                             start date                         </label>                         <input class="form-control" id="startdate" name="startdate" placeholder="dd/mm/yyyy"                                type="text" required=""/>                     </div>                     <div class="form-group ">                         <label class="control-label " for="enddate">                             end date                         </label>                         <input class="form-control" id="enddate" name="enddate" placeholder="dd/mm/yyyy" type="text"                                required=""/>                     </div>                     <div class="form-group">                         <div class="col-sm-10 col-sm-offset-2">                             <button class="btn btn-success btn-lg" id="submitbtn">                                 submit request                             </button>                         </div>                     </div>                     <div class="form-group">                         <div class="col-sm-10 col-sm-offset-2">                             <div id="form_result"></div>                         </div>                     </div>                 </form>             </div>         </div>     </div> </div> 

and api:

@restcontroller public class addholidayrequestcontroller {      @autowired     private addholidayservice addholidayservice;      @autowired     private holidayrepository holidayrepository;      @responsebody     @requestmapping(value = "/api/holidays", method = requestmethod.post)     public responseentity addholidayrequest(@requestbody holidayformdata holidayformdata) {         holidayrequest holidayrequest = new holidayrequest();          try {             holidayrequest = addholidayservice.isvalid(holidayformdata);         } catch (usernotfoundexception e) {             system.err.println(e.getmessage());         }         if (holidayrequest == null)             return new responseentity(httpstatus.bad_request);          holidayrepository.save(holidayrequest);          return new responseentity(httpstatus.created);     } } 

whenever make post request rest controller "error creating request" message error function, why ?

by looking code, seems me below line of code causing issue,

       $(this).prop('display', 'hidden'); 

inside success, $(this) wont refers current element can save reference of $(this) outside , can call use below

. . var $this = $(this); $.ajax({         method: "post",         url: "/api/holidays",         data: request,         contenttype: "application/json",         datatype: "json",         success: function () {             $("#form_result").text("submitted succesfully");             $this.prop('display', 'hidden');         },         error: function (error) {             $("#form_result").text("error: creating request");             $("#submitbtn").prop('disabled', false);         }     }); 

let me know if still not working...


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -