获取Jquery $ form.submit()调用的结果

时间:2020-08-09 23:57:17

标签: javascript jquery jsp

我有一个要添加jquery .attr值的表单,然后调用Submit:

$form.attr("method", "POST");
$form.attr("action", "test.jsp");
$form.attr("target", "blank");
$form.submit();

我想知道是否有办法让我从我调用的jsp文件中获得响应?

类似

$ form.submit()。response?

谢谢

1 个答案:

答案 0 :(得分:0)

如果您希望在不刷新页面的情况下获得响应,则需要执行AJAX请求。这可以通过执行以下操作来完成:

$(function(){
    $( "form" ).on( "submit", function( event ) {
        event.preventDefault();

        // If you want to serialze the form, you can use $(this).serialize() or $("form").serialize();
        $.post(
            '/test.jsp',
            $(this).serialize(),
            function(response) { console.log('response was:',response); }
        );

        // OR if you want to specify each form element
        $.post(
            '/test.jsp',
            {
                name: $('#name').val(),
                email: $('#email').val(),
            },
            function(response) { console.log('response was:',response); }
        );

    });
});

相关问题