js ajax XMLHttpRequest與jq $.ajax

來源:互聯網
上載者:User

xmlhttprequest 對象的方法與屬性
 

方    法
描    述
abort()
停止當前請求
getallresponseheaders()
把http請求的所有響應首部作為鍵/值對返回
getresponseheader("header")
返回指定首部的串值
open("method", "url")
建立對伺服器的調用。method參數可以是get、post或put。url參數可以是相對url或絕對url。這個方法還包括3個可選的參數
send(content)
向伺服器發送請求
setrequestheader("header", "value")
把指定首部設定為所提供的值。在設定任何首部之前必須先調用open()
  屬  性
描    述
onreadystatechange
每個狀態改變時都會觸發這個事件處理器,通常會調用一個網頁特效函數
readystate
請求的狀態。有5個可取值:0 = 未初始化,1 = 正在載入,2 = 已載入,3 = 互動中,4 = 完成
responsetext
伺服器的響應,表示為一個串
responsexml
伺服器的響應,表示為xml。這個對象可以解析為一個dom對象
status
伺服器的http狀態代碼(200對應ok,404對應not found(未找到),等等)
statustext
http狀態代碼的相應文本(ok或not found(未找到)等等)

<script type="text/javascript">
6 var req;
7         function sendtime() {
8             var url = "ajaxserver.asp教程x";
9             if (window.xmlhttprequest) //非ie瀏覽器及ie7(7.0及以上版本),用xmlhttprequest對象建立
10             {
11                 req = new xmlhttprequest();
12             }
13             else if (window.activexobject) //ie(6.0及以下版本)瀏覽器用activexobject對象建立,如果使用者瀏覽器禁用了activex,可能會失敗.
14             {
15                 req = new activexobject("microsoft.xmlhttp");
16             }
17             if (req) {
18                 xmlhttp.open("post", url, false);
19                 xmlhttp.onreadystatechange = function () {
20                     if (xmlhttp.readystate == 4) {
21                         if (xmlhttp.status == 200) {
22                             document.getelementbyid("txttime").value = xmlhttp.responsetext;
23                         }
24                         else {
25                             alert('ajax錯誤');
26                         }
27                     }
28                 }
29                 xmlhttp.send();
30             }
31         }
32
33     </script>
34 </head>
35 <body>
36     <input id="txttime" type="text" /><input id="btn1" type="button" value="ajax" onclick="sendtime()" />
37 </body>
jquery ajax

ajax.request(
url,
{
method:method,
parameters:para,
postbody:xmlstring,
asynchronous:true,
setrequestheader:object,
oncomplete:completefun,
onerror:errorfun
}
)
發送非同步請求。(此方法是為相容 prototype.js 而寫,調用風格與 prototype 一致,使用ajax.request此方法請在頁面中載入此js檔案)

參數
url
必選項。資料發送的目標地址。
method
可選項。資料提交的方式,預設值為get。常用的還有post。
parameters
當 method 為 get 時是可選項,為 post 時是必選項。發送的資料,其形式為: name1=valeu1& name2=value2&name3=value3......
postbody
可選項。用戶端發送的 xml 格式字串。如果啟用 postbody,那麼 parameters 將被忽略。
asynchronous
可選項。指定請求是否非同步,預設為true(非同步)。
setrequestheader
指定請求的頭部字串。其實值型別為“名稱值對”形式的對象,比如:{"if-modified-since":"0", "soapaction":"http://tempuri.org/sbs_webservice", ... ... }
oncomplete
可選項。請求成功時執行的回呼函數,該函數預設把當前使用 xmlhttp 對象作為第一個參數。
onerror
可選項。請求異常時執行的回呼函數,該函數預設把當前使用 xmlhttp 對象作為第一個參數。
傳回值
當前使用的 xmlhttp 對象。

描述
發送非同步請求,並返回 xmlhttp 對象,該對象內建有 abort() 方法,用於提前終止請求。非同步請求成功則執行 oncomplete,失敗則執行 onerror 。並返回 xmlhttp 對象。
ajax.request 是個介面完整的 ajax 方法,是 myjsframe 中所有其他 ajax 方法的核心方法。

樣本
樣本一:
<script type="text/javascript" >
   var myajax = new ajax.request(
        "http://www.111cn.net ",
        {
            method:"post",       //表單提交方式
            parameters:"name=acai&age=26&sex=male",   //提交的表單資料
            setrequestheader:{"if-modified-since":"0"},     //禁止讀取快取資料
            oncomplete:function(x){    //提交成功回調
                    alert(x.responsetext);
            },
            onerror:function(x){          //提交失敗回調
                    alert(x.statustext);
            }
        }
   );
</script>


註:parameters 參數若是不列出,我們在開發中對於form 表單這樣的資料 可以這樣處理

 parameters:form.serialize('formname')   formname  為頁面中表單的 id

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.