首页 > 解决方案 > 获取 Jquery $form.submit() 调用的结果

问题描述

我有一个表格,我要使用 jquery .attr 值添加,然后调用提交:

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

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

就像是

$form.submit().response?

谢谢

标签: javascriptjqueryjsp

解决方案


如果您希望在不刷新页面的情况下获得响应,则需要执行 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); }
        );

    });
});


推荐阅读