Intercept form POST string and send via AJAX instead

后端 未结 3 1008
名媛妹妹
名媛妹妹 2020-12-16 00:12

Is it possible to intercept a form\'s POST string and send it via AJAX instead? I could use $(\'form\').submit() to intercept the POST event, but I don\'t see where I can ge

3条回答
  •  感情败类
    2020-12-16 00:37

    You can do of course - you prevent your form from submitting as usual, you take its data and perform a POST via jQuery:

    $(form).submit(function(event){
    
        // prevents default behaviour, i.e. reloading the page
        event.preventDefault();
    
        $.post(
    
            $(this).attr('action'), // the form's action
            $(this).serializeArray(),   // the form data serialized
            function(data)
            {
    
                // what you are supposed to do with POST response from server
    
            }
    
        )
    
    });
    

提交回复
热议问题