Home  >  Article  >  Web Front-end  >  jquery下异步提交表单 异步跨域提交表单_jquery

jquery下异步提交表单 异步跨域提交表单_jquery

WBOY
WBOYOriginal
2016-05-16 18:16:141277browse

1.使用post提交方式
2.构造表单的数格式
3.结合form表单的submit调用ajax的回调函数。
使用 jQuery 异步提交表单代码:

复制代码 代码如下:



无标题页


<script> <BR>jQuery(function($) { <BR>// 使用 jQuery 异步提交表单 <BR>$('#f1').submit(function() { <BR>$.ajax({ <BR>url: 'ta.aspx', <BR>data: $('#f1').serialize(), <BR>type: "post", <BR>cache : false, <BR>success: function(data) <BR>{alert(data);} <BR>}); <BR>return false; <BR>}); <BR>}); <BR></script>










如何异步跨域提交表单呢?
1.利用script 的跨域访问特性,结合form表单的数据格式化,所以只能采用get方式提交,为了安全,浏览器是不支持post跨域提交的。
2.采用JSONP跨域提交表单是比较好的解决方案。
3.也可以动态程序做一代理。用代理中转跨域请求。
使用 jQuery 异步跨域提交表单代码:
复制代码 代码如下:



无标题页


<script> <BR>jQuery(function($) <BR>{ <BR>// 使用 jQuery 异步跨域提交表单 <BR>$('#f1').submit(function() <BR>{ <BR>$.getJSON("ta.aspx?"+$('#f1').serialize()+"&jsoncallback=?", <BR>function(data) <BR>{ <BR>alert(data); <BR>}); <BR>return false; <BR>}); <BR>}); <BR></script>









Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn