ajax與json 獲取數(shù)據(jù)并在前臺使用簡單實例
更新時間:2017年01月19日 14:58:47 投稿:lqh
這篇文章主要介紹了ajax與json 獲取數(shù)據(jù)并在前臺使用簡單實例的相關資料,需要的朋友可以參考下
用ajax獲取后臺數(shù)據(jù),返回json數(shù)據(jù),怎么在前臺使用呢?
后臺
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\":無用戶,\"id\":\"0\",\"company\":\"無用戶\"}]"); } else { context.Response.Write(s); } } }
前臺
$(document).ready(function () { $("#Button3").click( function (SucCallback) { $.ajax( { type: "get", url: 'GridDatas.ashx', //后臺處理程序 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ù)是列表還是單個了,就一條就無需中括號了
context.Response.Write("{\"name\":無用戶,\"id\":\"0\",\"company\":\"無用戶\"}"); $(document).ready(function () { $("#Button3").click( function (SucCallback) { $.ajax( { type: "get", url: 'GridDatas.ashx', //后臺處理程序 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"); } }); }) })
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
jquery實現(xiàn)隱藏與顯示動畫效果/輸入框字符動態(tài)遞減/導航按鈕切換
jquery實現(xiàn)隱藏顯示層動畫效果、仿新浪字符動態(tài)輸入、tab效果等等,以下為所有代碼,感興趣的朋友可以練練手哈,希望對大家學習有所幫助2013-07-07輕松學習jQuery插件EasyUI EasyUI創(chuàng)建樹形網(wǎng)絡(1)
這篇文章主要幫助大家輕松學習jQuery插件EasyUI,并且教大家如何利用EasyUI創(chuàng)建樹形網(wǎng)絡,感興趣的小伙伴們可以參考一下2015-11-11jQuery插件HighCharts實現(xiàn)氣泡圖效果示例【附demo源碼】
這篇文章主要介紹了jQuery插件HighCharts實現(xiàn)氣泡圖效果,結合完整實例形式分析了jQuery插件HighCharts繪制氣泡圖的實現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03Jquery實現(xiàn)無縫向上循環(huán)滾動列表的特效
今天小編就為大家分享一篇關于Jquery實現(xiàn)無縫向上循環(huán)滾動列表的特效,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02