ajax提交大數(shù)據(jù)文章

字號:


    在之前的文章中說過ajax,寫過詳細(xì)的步驟,http://www.anypoetry.com/html/1725.html
    上面的ajax寫的是GET方式提交的異步內(nèi)容,當(dāng)遇到大數(shù)據(jù)的情況下,會出現(xiàn)url超長,或者當(dāng)url的內(nèi)容出現(xiàn)“&”時,則會按照url的鏈接符號傳輸,會出現(xiàn)截斷。
    下面是ajax用POST的形式進(jìn)行提交,在提交大數(shù)據(jù)的內(nèi)容出現(xiàn)"&",不會出現(xiàn)錯誤或者數(shù)據(jù)截斷。
    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try{ // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
    catch (e){
    // Internet Explorer
    try{
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e){
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    }
    return xmlHttp;
    }
    function saveHint()
    { xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null){
    alert ("您的瀏覽器不支持AJAX!");
    return;
    }
    var url="anypoetry.com.save.php";
    data="p="+encodeURIComponent(document.getElementById('v').value);//定義參數(shù)p
    //主要是上面的這句 “encodeURIComponent”,設(shè)置將內(nèi)容進(jìn)行轉(zhuǎn)碼,這個非常重要。
    data=data+"&sid="+Math.random();//加上隨機(jī)碼,防止緩存
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.send(data);//將數(shù)據(jù)進(jìn)行post提交
    }
    function stateChanged()
    {
    if (xmlHttp.readyState==4&&xmlHttp.status == 200)
    {
    document.getElementById("v").value=xmlHttp.responseText;//獲取返回值,并給對象v
    }
    }
    接受頁面通過進(jìn)行傳統(tǒng)的接受即可,獲取前面的參數(shù)p
    比如php $_POST['p']
    asp request.form('p')
    這樣就可以完成大數(shù)據(jù)量的POST提交,并且能夠自動轉(zhuǎn)碼進(jìn)行url的&號規(guī)避。