ajax與json 獲取數(shù)據(jù)并在前臺(tái)使用簡(jiǎn)單實(shí)例
用ajax獲取后臺(tái)數(shù)據(jù),返回json數(shù)據(jù),怎么在前臺(tái)使用呢?
后臺(tái)
if (dataType == "SearchCustomer") { int ID; if (Int32.TryParse(CustomerID, out ID)) { string s = GridComputer.GridCustomer.getCustomer(1, 1, ID); if (s == null) { context.Response.ContentType = "text/plain"; context.Response.Write("[{\"name\":無(wú)用戶,\"id\":\"0\",\"company\":\"無(wú)用戶\"}]"); } else { context.Response.Write(s); } } }
前臺(tái)
$(document).ready(function () { $("#Button3").click( function (SucCallback) { $.ajax( { type: "get", url: 'GridDatas.ashx', //后臺(tái)處理程序 dataType: 'json', //接受數(shù)據(jù)格式 data: 'DataType=SearchCustomer&CustomerID=' + document.getElementById("Text3").value, //要傳遞的數(shù)據(jù) success:SucCallback, error: function () { alert("error"); } }); }) })
參考代碼
grid.getCustomer(1,2,function (data) { var list = '<p>' + tree_GridInfo._name + '的用戶有</p><br>'; list += '<table id="customers"><tr><th>姓名</th><th>電話</th></tr> '; $.each(data, function (i, n) { list += '<tr onclick="showUser(' + 1 + ')"><td>'; list += n.name + '</td>' + '<td>' + n.company; list += '</td></tr>'; }); $("#SearchResult").html(list)
看你的json數(shù)據(jù)是列表還是單個(gè)了,就一條就無(wú)需中括號(hào)了
context.Response.Write("{\"name\":無(wú)用戶,\"id\":\"0\",\"company\":\"無(wú)用戶\"}"); $(document).ready(function () { $("#Button3").click( function (SucCallback) { $.ajax( { type: "get", url: 'GridDatas.ashx', //后臺(tái)處理程序 dataType: 'json', //接受數(shù)據(jù)格式 data: 'DataType=SearchCustomer&CustomerID=' + document.getElementById("Text3").value, //要傳遞的數(shù)據(jù) function (dataJson) { alert(dataJson.Name); alert(dataJson.Id); }, error: function () { alert("error"); } }); }) })
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
jquery實(shí)現(xiàn)的分頁(yè)顯示功能示例
這篇文章主要介紹了jquery實(shí)現(xiàn)的分頁(yè)顯示功能,涉及jQuery的ajax交互及頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-08-08jquery實(shí)現(xiàn)隱藏與顯示動(dòng)畫效果/輸入框字符動(dòng)態(tài)遞減/導(dǎo)航按鈕切換
jquery實(shí)現(xiàn)隱藏顯示層動(dòng)畫效果、仿新浪字符動(dòng)態(tài)輸入、tab效果等等,以下為所有代碼,感興趣的朋友可以練練手哈,希望對(duì)大家學(xué)習(xí)有所幫助2013-07-07輕松學(xué)習(xí)jQuery插件EasyUI EasyUI創(chuàng)建樹形網(wǎng)絡(luò)(1)
這篇文章主要幫助大家輕松學(xué)習(xí)jQuery插件EasyUI,并且教大家如何利用EasyUI創(chuàng)建樹形網(wǎng)絡(luò),感興趣的小伙伴們可以參考一下2015-11-11jQuery插件HighCharts實(shí)現(xiàn)氣泡圖效果示例【附demo源碼】
這篇文章主要介紹了jQuery插件HighCharts實(shí)現(xiàn)氣泡圖效果,結(jié)合完整實(shí)例形式分析了jQuery插件HighCharts繪制氣泡圖的實(shí)現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03Jquery實(shí)現(xiàn)無(wú)縫向上循環(huán)滾動(dòng)列表的特效
今天小編就為大家分享一篇關(guān)于Jquery實(shí)現(xiàn)無(wú)縫向上循環(huán)滾動(dòng)列表的特效,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02使用JQuery庫(kù)提供的擴(kuò)展功能實(shí)現(xiàn)自定義方法
如何沒(méi)有任何一個(gè)庫(kù)可以滿足所有的需求,可以使用JQuery庫(kù)提供的擴(kuò)展功能,下面有個(gè)示例,大家可以看看2014-09-09