整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

Ajax提交数据

Ajax提交数据

  1. 在业务上面使用到了ajax上传图片,采用的方式是提供一个公用的api进行图片上传,然后返回图片的在服务器的url,这样在其他地方使用到时,直接提交图片的url,而不是图片资源,避免影响应能和体验,也方便后期切换(如果后期采用了第三方图片服务,或者对图片需要进行处理,只要改造这个接口就好了)。
  2. 使用Ajax提交表单数据(包含上传文件)有两种形式
  • using only AJAX
  • using the FormData API
  1. 优缺点
  • Using the FormData API is the simplest and fastest, but has the disadvantage that data collected can not be stringified.
  • Using only AJAX is more complex, but typically more flexible and powerful.

FormData简介

 The FormData object lets you compile a set of key/value pairs to send using XMLHttpRequest. It is primarily intended for use in sending form data, but can be used independently from forms in order to transmit keyed data. The transmitted data is in the same format that the form's submit() method would use to send the data if the form's encoding type were set to multipart/form-data.

大致意思是你可以将数据使用FormData对象编译成键值对形式,然后使用XMLHttpRequest技术向后端发送数据。主要是用来发送form表单数据,也可以发送带键数据。这种形式传输的数据和一个enctype属性为multipart/form-data并且采用submit()方法提交的form表单传输的数据格式相同。

Ajax使用FormData提交数据

只是简单的示范一下文件上传,表单数据类似,不写例子了。

Ajax上传文件-带form标签的FormData提交

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>测试</title>
</head>
<body>
<form id="upload" method="post">
 <input id="file" type="file" name="file"/>
 <input id="img" type="hidden"/>
 <input type="submit" value="上传图片">
</form>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
var BASE_URL='../'
$(document).ready(function(){
 $('#file').on('change', function() {
 var formData=new FormData($('#upload')[0])
 $.ajax({
 url: BASE_URL + 'api/upload',
 type: 'post',
 cache: false,
 data: formData,
 processData: false,
 contentType: false,
 success: function(res) {
 console.log(res)
 }
 })
 })
})
</script>
</body>
</html>
<?php
print_r($_FILE);exit();
?>

特点:form表单提交,带有<form>标签,enctype="multipart/form-data"不设置也可以。

Ajax不带form标签的FormData提交

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>测试</title>
</head>
<body>
 <input id="file" type="file" name="file"/>
 <input id="img" type="hidden"/>
 <input type="submit" value="上传图片">
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
var BASE_URL='../'
$(document).ready(function(){
 $('#file').on('change', function() {
 console.log(this.files)
 var formData=new FormData()
 formData.append("file", this.files[0]);
 $.ajax({
 url: BASE_URL + 'api/upload',
 type: 'post',
 cache: false,
 data: formData,
 processData: false,
 contentType: false,
 success: function(res) {
 console.log(res)
 }
 })
 })
})
</script>
</body>
</html>
<?php
print_r($_FILE);exit();
?>

没有<form>标签,基本使用场景中使用的是这种。

Ajax不使用FormData提交数据

从参考2来看,上传文件需要使用使用FileReader对象,并且Ajax不使用FormData提交数据略复杂,幸亏有一些大咖封装了一下,比如官方提供了一个A little vanilla framework(一点香草??????这个使用原生写的,不是封装,,,),再比如ajaxFileUpload(github地址是参考5,官方有示例,试了一下,妥妥的支持IE6..)。

感受

FormData是HTML5新增的属性,可能在兼容浏览器上面会抛弃一些古典(old)浏览器,不过简单;利用纯Ajax提交也还好,因为有很多现成的库,比如jquery,axios...从A little vanilla framework的示例(参考2)来看,基本是根据form表单的encypt形式,采用相应的方式发送数据。

参考

  1. https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
  2. https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Submitting_forms_and_uploading_files
  3. https://developer.mozilla.org/en-US/docs/Web/API/FileReader
  4. https://www.cnblogs.com/zhuxiaojie/p/4783939.html
  5. https://github.com/davgothic/AjaxFileUpload

JAX 是一种与服务器交换数据的技术,可以在补充在整个页面的情况下更新网页的一部分。接下来通过本文给大家介绍ajax一些常用方法,大家有需要可以一起学习。

1.url:

要求为String类型的参数,(默认为当前页地址)发送请求的地址。

2.type:

要求为String类型的参数,请求方式(post或get)默认为get。注意其他http请求方法,例如put和delete也可以使用,但仅部分浏览器支持。

3.timeout:

要求为Number类型的参数,设置请求超时时间(毫秒)。此设置将覆盖$.ajaxSetup()方法的全局设置。

4.async:

要求为Boolean类型的参数,默认设置为true,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为false。注意,同步请求将锁住浏览器,用户其他操作必须等待请求完成才可以执行。


5.cache:

要求为Boolean类型的参数,默认为true(当dataType为script时,默认为false),设置为false将不会从浏览器缓存中加载请求信息。

6.data:

要求为Object或String类型的参数,发送到服务器的数据。如果已经不是字符串,将自动转换为字符串格式。get请求中将附加在url后。防止这种自动转换,可以查看 processData选项。对象必须为key/value格式,例如{foo1:"bar1",foo2:"bar2"}转换为&foo1=bar1&foo2=bar2。如果是数组,JQuery将自动为不同值对应同一个名称。例如{foo:["bar1","bar2"]}转换为&foo=bar1&foo=bar2。

7.dataType:

要求为String类型的参数,预期服务器返回的数据类型。如果不指定,JQuery将自动根据http包mime信息返回responseXML或responseText,并作为回调函数参数传递。可用的类型如下:

xml:返回XML文档,可用JQuery处理。

html:返回纯文本HTML信息;包含的script标签会在插入DOM时执行。

script:返回纯文本JavaScript代码。不会自动缓存结果。除非设置了cache参数。注意在远程请求时(不在同一个域下),所有post请求都将转为get请求。

json:返回JSON数据。

jsonp:JSONP格式。使用SONP形式调用函数时,例如myurl?callback=?,JQuery将自动替换后一个“?”为正确的函数名,以执行回调函数。

text:返回纯文本字符串。

8.beforeSend:

要求为Function类型的参数,发送请求前可以修改XMLHttpRequest对象的函数,例如添加自定义HTTP头。在beforeSend中如果返回false可以取消本次ajax请求。XMLHttpRequest对象是惟一的参数。

function(XMLHttpRequest){

this; //调用本次ajax请求时传递的options参数

}

9.complete:

要求为Function类型的参数,请求完成后调用的回调函数(请求成功或失败时均调用)。参数:XMLHttpRequest对象和一个描述成功请求类型的字符串。

function(XMLHttpRequest, textStatus){

this; //调用本次ajax请求时传递的options参数

}

10.success:

要求为Function类型的参数,请求成功后调用的回调函数,有两个参数。

(1)由服务器返回,并根据dataType参数进行处理后的数据。

(2)描述状态的字符串。

function(data, textStatus){

//data可能是xmlDoc、jsonObj、html、text等等

this; //调用本次ajax请求时传递的options参数

}

11.error:

要求为Function类型的参数,请求失败时被调用的函数。该函数有3个参数,即XMLHttpRequest对象、错误信息、捕获的错误对象(可选)。ajax事件函数如下:

function(XMLHttpRequest, textStatus, errorThrown){

//通常情况下textStatus和errorThrown只有其中一个包含信息

this; //调用本次ajax请求时传递的options参数

}

12.contentType:

要求为String类型的参数,当发送信息至服务器时,内容编码类型默认为"application/x-www-form-urlencoded"。该默认值适合大多数应用场合。

13.dataFilter:

要求为Function类型的参数,给Ajax返回的原始数据进行预处理的函数。提供data和type两个参数。data是Ajax返回的原始数据,type是调用jQuery.ajax时提供的dataType参数。函数返回的值将由jQuery进一步处理。

function(data, type){

//返回处理后的数据

return data;

}

14.dataFilter:

要求为Function类型的参数,给Ajax返回的原始数据进行预处理的函数。提供data和type两个参数。data是Ajax返回的原始数据,type是调用jQuery.ajax时提供的dataType参数。函数返回的值将由jQuery进一步处理。

function(data, type){

//返回处理后的数据

return data;

}

15.global:

要求为Boolean类型的参数,默认为true。表示是否触发全局ajax事件。设置为false将不会触发全局ajax事件,ajaxStart或ajaxStop可用于控制各种ajax事件。

16.ifModified:

要求为Boolean类型的参数,默认为false。仅在服务器数据改变时获取新数据。服务器数据改变判断的依据是Last-Modified头信息。默认值是false,即忽略头信息。

17.jsonp:

要求为String类型的参数,在一个jsonp请求中重写回调函数的名字。该值用来替代在"callback=?"这种GET或POST请求中URL参数里的"callback"部分,例如{jsonp:'onJsonPLoad'}会导致将"onJsonPLoad=?"传给服务器。

18.username:

要求为String类型的参数,用于响应HTTP访问认证请求的用户名。

19.password:

要求为String类型的参数,用于响应HTTP访问认证请求的密码。

20.processData:

要求为Boolean类型的参数,默认为true。默认情况下,发送的数据将被转换为对象(从技术角度来讲并非字符串)以配合默认内容类型"application/x-www-form-urlencoded"。如果要发送DOM树信息或者其他不希望转换的信息,请设置为false。

21.scriptCharset:

要求为String类型的参数,只有当请求时dataType为"jsonp"或者"script",并且type是GET时才会用于强制修改字符集(charset)。通常在本地和远程的内容编码不同时使用。

案例代码:

$(function(){

$('#send').click(function(){

$.ajax({

type: "GET",

url: "test.json",

data: {username:$("#username").val(), content:$("#content").val()},

dataType: "json",

success: function(data){

$('#resText').empty(); //清空resText里面的所有内容

var html='';

$.each(data, function(commentIndex, comment){

html +='<div class="comment"><h6>' + comment['username']

+ ':</h6><p class="para"' + comment['content']

+ '</p></div>';

});

、为什么xml

需要服务器端返回少量的、单一的数据

用户名是否可用 1 / 0

返回两个数的和 400

登录是否成功 true/false

数据插是否成功 true/false

如果我们需要从服务器端返回大量、复杂的数据,如何实现?

xml:服务器端返回xml数据

json:服务器端返回json数据

2、格式:

(1)php解析xml

$dom=new DOMDocument();
$dom->loadXML($str);
$nd=$dom->getElementsByTagName("TagName");
$value=$nd->item(0)->nodeValue
$xml=simplexml_load_string($str);
$first=$xml->first;
$second=$xml->second;

(2)js解析xml

var xml=xmlHttp.responseXML;
node=xml.getElementsByTagName("TagName");
node[0].childNodes[0].nodeValue;

3 、案例1:

实现两个数的四则运算

HTML代码:

<script language="javascript" src="public.js"></script>
<script>
window.onload=function(){
        $('btnOk').onclick=function(){
        var f=$('first').value;
        var s=$('second').value;
        var data='first='+f+'&second='+s;
        var xhr=createxhr();
        xhr.open('post','demo01.php');
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.onreadystatechange=function(){
                if(xhr.readyState==4 && xhr.status==200){
                        //xml --->xml dom对象
                        var xml=xhr.responseXML;
                        var str=xml.getElementsByTagName('jia')[0].childNodes[0].nodeValue;
                        str+='|'+xml.getElementsByTagName('jian')[0].childNodes[0].nodeValue;
                        str+='|'+xml.getElementsByTagName('cheng')[0].childNodes[0].nodeValue;
                        str+='|'+xml.getElementsByTagName('chu')[0].childNodes[0].nodeValue;
                        $('result').innerHTML=str;
                }
        };
        xhr.send(data);
        };
};
</script>
<input type="text" id="first" /><br>
<input type="text" id='second' /><br>
<div id='result'></div>
<input type="button" id="btnOk" value="计算" />

理解:

var xml=xhr.responseXML; 得到ajax返回的xmldom对象

xml.getElementsByTagName('jia')[0] :是表示获取jia这个元素

xml.getElementsByTagName('jia')[0].childNodes:表示获取jia元素下的所有子节点

xml.getElementsByTagName('jia')[0].childNodes[0] :表示获取jia元素下的唯一文本节点

xml.getElementsByTagName('jia')[0].childNodes[0].nodeValue:文本节点的值

php代码:

<?php
$first=$_POST['first'];
$second=$_POST['second'];
$result1=$first+$second;
$result2=$first-$second;
$result3=$first*$second;
$result4=$first/$second;
//要想返回xml,首先连接一个xml格式的字符串
$str='<root>';
$str.='<jia>'.$result1.'</jia>';
$str.='<jian>'.$result2.'</jian>';
$str.='<cheng>'.$result3.'</cheng>';
$str.='<chu>'.$result4.'</chu>';
$str.='</root>';
/*$str=<<<str
<root>
<jia>$result1</jia>
</root>
str;*/
header('Content-type:text/xml');
echo $str;

理解:

得到结果后,需要使用字符串连接成一个xml格式的字符串,如:需要一个根元素,下面子元素,最后是具体的值,

连接时也可以使用<<<str创建xml字符串

str;

输出这个字符串时,默认的响应内容类型:text/html,也就是说客户端仍把代码当做html来进行解析,

ajax对象的responeXML是不能得到一个xmldom对象,必须设置响应头类型为:text/xml,其代码:header('Content-type:text/xml');

public.js:

function createxhr() {
        /*var xhr;
        var str=window.navigator.userAgent;
        if (str.indexOf('MSIE') > 0) {
        xhr=new ActiveXObject('Microsoft.XMLHTTP');
        } else {
        xhr=new XMLHttpRequest();
        }
        return xhr;*/

        try{return new XMLHttpRequest();}catch(e){}
        try{return new ActiveXObject('Microsoft.XMLHTTP'); }catch(e){}
        alert('请更换浏览器!');
        }
        function $(id){
        return document.getElementById(id);
}

4、案例2

在页面加载之后,将mysql数据库goods表中所有数据显示在表格中

<root>
<goods>
<name>222</name>
<price>55.00</price>
</goods>
<goods>
<name>诺 E661</name>
<price>205.00</price>
</goods>
<goods>
<name>诺 E661</name>
<price>200.00</price>
</goods>
</root>

HTML代码:

<style>
tr{
background-color:#ffffff;
height:30px;
font-size:12px;
}
</style>
<script language="javascript" src='public.js'></script>
<script>
window.onload=function(){
        var xhr=createxhr();
        xhr.open('post','demo02.php');
        xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        xhr.onreadystatechange=function(){
                if(xhr.readyState==4 && xhr.status==200)
                        var xml=xhr.responseXML;
                        var goods=xml.getElementsByTagName('goods');
                        for(var i=0;i<goods.length;i++){
                        //创建行元素
                        var tr=document.createElement('tr');
                        //创建序号td元素
                        var tdID=document.createElement('td');
                        tdID.innerHTML=i+1;
                        //创建名称td元素
                        var tdName=document.createElement('td');
                        tdName.innerHTML=goods[i].childNodes[0].childNodes[0].nodeValue;
                        //创建价格td元素
                        var tdPrice=document.createElement('td');
                        tdPrice.innerHTML=goods[i].childNodes[1].childNodes[0].nodeValue;
                        //将三个td追加到tr元素
                        tr.appendChild(tdID);
                        tr.appendChild(tdName);
                        tr.appendChild(tdPrice);
                        document.getElementsByTagName('TBODY')[0].appendChild(tr);
                }
};
xhr.send(null);
}
</script>
<table id='tbData' width="800" cellspacing="1" cellpadding="4" bgcolor="#336699">
<tr>
<td>序号</td>
<td>商品名称</td>
<td>商品价格</td>
</tr>
</table>

理解:

创建行元素,

创建单元格元素

将单元格元素追加到行元素中

将行元素追加到表格元素中

php代码:

<?php
        //查询goods表中所有数据并返回
        $sql="select name,price from goods order by id desc";
        mysql_connect('localhost','root','111111');
        mysql_select_db('shop');
        mysql_query('set names gb2312');
        $result=mysql_query($sql); //发送sql语句
        $num=mysql_num_rows($result); //总行数
        $str='<root>';
        for($i=0;$i<$num;$i++){
        $row=mysql_fetch_assoc($result);
        $str.='<goods>';
        $str.='<name>'.iconv('gb2312','utf-8',$row['name']).'</name>';
        $str.='<price>'.$row['price'].'</price>';
        $str.='</goods>';
        }
        $str.='</root>';
        header('Content-Type:text/xml');
        echo $str;
?>

理解:

查询goods表中所有数据

连接xml格式的字符串

表中有多少条数据

xml字符串就有几对goods标签

其中, name字段出现中文,所以需要进行转码 gb2312--utf-8

最后, 输出xml字符串