Ajax,純Js+Jquery

來源:互聯網
上載者:User

標籤:發送   支援   get   通過   state   cat   ...   class   發送post請求   

AJAX:Asynchronous Javascript and xml 非同步,Js和Xml 互動式網頁開發 不重新整理頁面,與伺服器互動 詳情請參照Jquery工具指南
用在瀏覽器端的技術,無重新整理,通過XmlHttpRequest訪問頁面
純js版----------

if(XmlHttpRequest){ //判斷是否支援XmlHttpRequestxhr= new XmlHttpRequest(); // 建立XmlHttpRequest對象}else{xhr= new Activexobject("Microsoft.XMLHTTP"); //不支援XmlHttpRequest,使用此方法建立 }xhr.open("get|post","url",true);xhr.send(); // 開始發送xhr.onreadystatechange= function(){ // 回呼函數,當伺服器將資料返回給瀏覽器觸發方法if(xhr.readyState==4) //0沒調用open方法,1表示未調用send方法,2正在等待狀態代碼和頭的返回,3 已接受部分資料,但還沒接受完,不能使用該對象的屬性和方法,4已載入,所有資料執行完畢if(xhr.status==200){ // 響應狀態代碼,表示頁面執行無誤alert(xhr.responseText); // 輸出接受到的文本}}

  


發送post請求


在send中寫資料。並添加請求前序

xhr.setRequestHeader("Content-Type",application/x-www-form-urlencoded)xhr.send("id=123&pwd=456");

  


-------------------------------------
Jquery版
強大的Jquery。。只需要get頁面就夠了

$.get("url",{"id" : "123","pwd":456},function(data)){ //自動把參數當做get請求傳輸alert(data)}

  


只需要post頁面。

$.post("url",{"id" : 123,"pwd":456},function(data)){ //post請求alert(data)}

  


//第三種寫法

$.ajax({type="get"| post,url="...",data:"參數",success:function(msg){...} // msg為從伺服器接受到的資料})

  

Ajax,純Js+Jquery

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.