[Fixed]-Ajax in django cannot work when submit the form

1👍

make use of dedicated way in jquery;

 // code will activated when you try to submit the form which has id tool_form
 $(document).on('submit','#tool_form',function(e){
    // will prevent to redirect the page
    e.preventDefault();
    // frm is the object variable which will hold the current form which is submitting
    var frm = $(this);
    $.ajax({
         // $(frm) is the object which is submitting 
         method:$(frm).attr('method'),
         url:$(frm).attr('action'),
         data:$(frm).serialize(),
         success:function(data){
                var rlst= $("#run_result");
                $(rlst).html(data);
            },
         error:function(data){
                alert("unknown error");
         }
        });
        alert("done");
   });

Leave a comment