jQuery與ajax

來源:互聯網
上載者:User

標籤:變數   empty   value   方式   bsp   attr   ext   jquery   jquery代碼   

$.get():使用get方式進行非同步請求

$.get(url,data/{"param1":"pamaram1value",},callback,type)

$.get()方法的callback只有兩個參數:

function(data,textStatus){

//data

//textStatus

}

data參數代表請求返回的內容,textStatus代表返回請求的狀態

返回xml文檔:當伺服器端返回的資料格式是xml文檔,因此需要對返回的資料進行處理,處理xml文檔和html文檔一樣,也可以使用常規的attri().find().fliter()以及其他方法,jQuery代碼如下:

$(function(){

$("selector").click(function(){

$.get("url",{

param1:$("#param1").val(),

param2:$("#param2").val()

},function(data,textStatus){

var para1=data.param1;

var para2=data.param2;

var txtHtml="<div class=‘para1‘>"+para1+"</div>"

},json);

})

})

$.getScript()和$.getJson()

$(function(){

$("selector").click(function(){

$.getScript(‘test.js‘)

})

})

 

$.getJSON(),用於載入json檔案

可以在函數中通過data變數來遍曆相應的資料,也可以使用迭代的方式為每個項構建相應的html代碼,雖然也可以用傳統的for迴圈來實現,jQuery提供了一個通用的遍曆方法$.each(),可以用於遍曆對象和數組,$.each()函數不同於jquery 對象的each() 方法,它是一個全域函數,不操作jquery對象,而是以第一個為對象的成員或數組的索引,第二個為對應變數或內容

$(function(){

$("selector").click(function(data){

$("selector").empty();

var html="";

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

html+=‘<div class="comment"><h6>‘+comment[‘username‘]+‘:</h6><p class=‘para‘>

})

})

})

 

jQuery與ajax

聯繫我們

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