AJAX-----13HTML5中新增的API---FormData
FormData 表单数据对象,这是在HTML5中新增的一个API,他能以表单对象做参数,自动的将表单的数据打包,当ajax发送数据是,发送FormData内的表单数据给后端即可

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<script>
function fd(){
var fdate = document.getElementById('fdate');
var fd = new FormData(fdate);
var xhr = new XMLHttpRequest();
xhr.open('post','10.php',true);
xhr.send(fd);
xhr.onreadystatechange = function(){
if(this.readyState == 4){
document.getElementById('desc').innerHTML = this.responseText;
}
} }
</script>
<body>
<form id="fdate" >
user:<input type="text" name="user"> <br><br>
age:<input type="text" name="age"> <br><br>
sex:<input type="text" name="sex"> <br><br>
QQ:<input type="text" name="QQ"> <br><br>
<input type="button" value="Go" onclick="fd();">
</form>
<p id="desc"></p>
</body>
</html>

效果如下所示:

这样比传统的拼接方法简单多了,当然他还有另外一种用法,如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<script>
function fd(){
var fdate = document.getElementById('fdate');
var fd = new FormData(fdate);
var fd2 = new FormData();
var xhr = new XMLHttpRequest();
xhr.open('post','10.php',true);
//xhr.send(fd);
fd2.append('user','leigood');
fd2.append('age','18');
xhr.send(fd2);
xhr.onreadystatechange = function(){
if(this.readyState == 4){
document.getElementById('desc').innerHTML = this.responseText;
}
} }
</script>
<body>
<form id="fdate" >
user:<input type="text" name="user"> <br><br>
age:<input type="text" name="age"> <br><br>
sex:<input type="text" name="sex"> <br><br>
QQ:<input type="text" name="QQ"> <br><br>
<input type="button" value="Go" onclick="fd();">
</form>
<p id="desc"></p>
</body>
</html>
效果如下所示:

AJAX-----13HTML5中新增的API---FormData的更多相关文章
- 第30月第11天 Xcode 9.0中新增的API版本检查@available
1.Xcode 9.0中新增的API版本检查@available https://www.jianshu.com/p/0a94baa6c3dd https://www.jianshu.com/p/b8 ...
- ASP.NET MVC4中调用WEB API的四个方法
http://tech.it168.com/a2012/0606/1357/000001357231_all.shtml [IT168技术]当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各 ...
- jquery ajax 请求中多出现一次OPTIONS请求及其解决办法
http://www.tangshuang.net/2271.html 在上一篇<服务端php解决jquery ajax跨域请求restful api问题及实践>中,我简单介绍了如何通过服 ...
- Django使用AJAX调用自己写的API接口
Django使用AJAX调用自己写的API接口 *** 具体代码和数据已上传到github https://github.com/PythonerKK/eleme-api-by-django-rest ...
- jQuery.ajax()方法中參数具体解析
前言 在项目开发中,为了实现异步向服务端发起请求,最常常使用的就是jQuery.ajax方法了.刚開始需求比較简单,调用jQuery.ajax方法时要传的參数也就那几个常见的參数:url/data/d ...
- .NET和.NET Core Web APi FormData多文件上传对比
前言 最近因维护.NET和.NET Core项目用到文件上传功能,虽说也做过,但是没做过什么对比,借此将二者利用Ajax通过FormData上传文件做一个总结,通过视图提交表单太简单,这里不做阐述,希 ...
- $.ajax 跨域请求 Web Api
WepApi确实方便好用,没有配置文件,一个apicontroller直接可以干活了.但今天用$.ajax跨域请求的时候总是获取不到数据,用fiddler一看确实抓到了数据,但回到$.ajax函数中, ...
- 以短链服务为例,探讨免AppKey、免认证、Ajax跨域调用新浪微博API
新浪微博的API官方提供了很多种调用方式,支持编程的,归根结底就是两种: 1.基于Oauth协议,使用Open API.(http://open.weibo.com/wiki/%E6%8E%88%E6 ...
- ionic 运行过程中动态切换API服务器地址
ionic 运行过程中动态切换API服务器地址 keywords: ionic,phonegap,cordova,网络制式,动态切换,变更,API,服务器地址,$resource,localstora ...
随机推荐
- Solr资料
Apache Solr Reference GuideCovering Apache Solr 5.5 https://archive.apache.org/dist/lucene/solr/ref- ...
- Linux下rar命令详解
Linux下rar命令详解 用法: rar <命令> -<选项1> ….-<选项N> < 操作文档> <文件…> <@文件列表…> ...
- 采用DOM进行表格的修改操作
2015-08-31 <html> <head> <title>采用DOM进行表格的修改操作</title> <script language=& ...
- 段落的展开收起(substring的应用)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 网页mp3播放代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- DateUtils
package com.vcredit.ddcash.batch.util; import java.text.SimpleDateFormat;import java.util.Calendar;i ...
- “迷宫”sprint——6.8
会议时间:2015.6.8 ,12:30——13:00 会议内容:开始第二阶段冲刺,分配任务. 我的任务:完成安卓环境搭建.
- 【iCore3 双核心板】例程六:IWDG看门狗实验——复位ARM
实验指导书及代码包下载: http://pan.baidu.com/s/1c0frjHm iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- ADB工具和手机抓包方法介绍
Android抓包方法 工具包内容如下:(下载地址:http://download.csdn.net/download/yezhaohui2011/8368061) adb ——谷歌提供的安卓远程调试 ...
- JQuery-文档处理&选择器
<body> <div id="textDiv">...</div> <p>第一段..</p> <b>第二段 ...