表单:数据的提交
- action:数据提交的地址,默认是当前页面
- method:数据提交的方式,默认是get方式
- enctype:提交的数据格式,默认是application/x-www-form-urlencoded
<form action='地址' method='post/get' enctype=‘application/x-www-form-urlencoded’>
<input type='text' name='username'/>
<input type='text' name='guo'/>
<input type='submit' value='提交'/>
<form>
1.get
把数据名称和数据值用=连接,如果有多个的话,那么它会把多个数据组合用&进行连接,然后把数据放到url?后面传到指定页面。
1. 有缓存问题 在url?后面连接一个随机数,时间戳
2. 中文乱码问题,编码encodeURI
xhr.open('get','2.php?username='+enCodeURI('程序员')+'&age=30&'+new Date().getTime(),true);
xhr.send();
2.post
- post方式,数据放在send()里面作为参数传递,需要设置请求头setRequestHeader申明发送的数据类型。
- post没有缓存问题
- 无需编码,无中文乱码问题
xhr.open('get','2.php',true);
xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
xhr.send('username=程序员&age=30');