拦截形成POST字符串并通过AJAX发送 [英] Intercept form POST string and send via AJAX instead

查看:173
本文介绍了拦截形成POST字符串并通过AJAX发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以截取表单的POST字符串并通过AJAX发送它?我可以使用$('form')。submit()拦截POST事件,但我看不到我可以从哪里获取POST字符串。我可以从表单的输入中重现字符串,但这看起来很可疑。

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 get the POST string from. I could reproduce the string from the form's inputs, but this seems dubious.

推荐答案

// capture submit
$('form').submit(function() {
     var $theForm = $(this);

     // send xhr request
     $.ajax({
         type: $theForm.attr('method'),
         url: $theForm.attr('action'),
         data: $theForm.serialize(),
         success: function(data) {
             console.log('Yay! Form sent.');
         }
     });

     // prevent submitting again
     return false;
});

请注意, Phil 在他的评论中说明 .serialize()不不包括提交按钮。如果你还需要提交buton的值,你必须手动添加它。

Note that as Phil stated in his comment that .serialize() doesn't include the submit button. If you also need to value of the submit buton you would have to add it manually.

这篇关于拦截形成POST字符串并通过AJAX发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆