简体   繁体   中英

Form submitting with jQuery Ajax

I am trying to submit a form with jquery and ajax, but code does not work. What am I doing wrong?

<script>
  $( function() {
    $( '#myForm' ).submit( function( evt ) {
      evt.preventDefault();
      var $form = $( this );
      $.ajax( {
        type: $form.prop( 'method' ),
        url: $form.prop( 'action' ),
        data: $form.serialize(),
        dataType: "json",
        traditional: true,
        success: function( response ) {
          alert("it works!");
          document.body.innerHTML = response;
        }
      });
    });
  });
</script> 

Solved my problem by changing code a bit:

I solved my problem by changing url,type.

And I settled to change div with id=mylist. Now it works.

<script>
                      $( function() {
                          $( '#paieska' ).submit( function( evt ) {
                              evt.preventDefault();   
                              $.ajax({
                                  url: "/Transport/Lentele",
                                  type: 'POST',
                                  data: $("#paieska").serialize(),
                                  success: function (response) {
                                      $("#mylist").html(response);                            
                                  }                           
                              });                                
                          });
                      });
                  </script>

I'll add this as solution when I will be available to.

what is the response code for this url(the one in AJAX). you check it inside Net, in developer tools.. also, by seeing this only, i can tell that you should not be using preventdefault, might be it starts working...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM