[英]Getting the result of a Jquery $form.submit() call
我有一個要使用 jquery.attr 值添加的表單,然后調用提交:
$form.attr("method", "POST");
$form.attr("action", "test.jsp");
$form.attr("target", "blank");
$form.submit();
我想知道是否有辦法從我調用的 jsp 文件中獲取響應?
就像是
$form.submit().response?
謝謝
如果您希望在不刷新頁面的情況下獲得響應,則需要執行 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); }
);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.