javascript - jquery ajax call redirects to called url insted of same template -
i have django form :
<form class="myform" action="{% url "create" %}" method="post">{% csrf_token %} <div class="results"></div> <label class="input margin-bottom-10">{{form.name}} <div class="name_error"></div> </label> <label class="input margin-bottom-10">{{form.age}} <div class="age_error"></div> </label> <input class="margin-top-10 pull-right" type="submit" value="confirm" > </form>
and jquery like:
frm = $('.myform') frm.on('submit', function(event){ create(frm); }); function create(frm) { $.ajax({ url : frm.attr('action'), // endpoint type : frm.attr('method'), // http method data: frm.serialize(), // data sent post request // handle successful response success : function(response) { if(response.status == "success"){ var success_message = "<div>some success message</div>) $('#container_body').html(success_message); }; if(response.status == "error"){ frm.find('.results').html("<div class='alert alert-mini alert-danger'>"+response.message+"</div>"); }; }, }); };
here ajax call doing good. getting proper response dom manipulation wrong..
when click submit button , there error. want show error message on .results
class.
when there error form error shown while , disappear , url page json reponse shown.
my form error message did not stay , redirects url page json response.
what wrong in here
your scripts had js errors , submitted. here need address this. read inline comments.
frm.on('submit', function(event){ event.preventdefault(); //prevents default action happening , rayon mentioned create(frm); });
fix js errors in success function. remember js errors may fail whole app.
success : function(response) { if(response.status == "success"){ var success_message = "<div>some success message</div>"; //this line had error in code $('#container_body').html(success_message); } //you don't need semicolon here if(response.status == "error"){ frm.find('.results').html("<div class='alert alert-mini alert-danger'>"+response.message+"</div>"); } //you don't need semicolon here }// dont need redundant comma here error out in ie
Comments
Post a Comment