ajax與json 獲取數(shù)據(jù)并在前臺使用簡單實例
更新時間:2017年01月19日 14:58:47 投稿:lqh
這篇文章主要介紹了ajax與json 獲取數(shù)據(jù)并在前臺使用簡單實例的相關(guān)資料,需要的朋友可以參考下
用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"); }
});
})
})
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
jQuery實現(xiàn)大轉(zhuǎn)盤抽獎活動仿QQ音樂代碼分享
這篇文章主要展示了jQuery實現(xiàn)大轉(zhuǎn)盤抽獎活動仿QQ音樂實現(xiàn)代碼,需要的朋友可以參考下2015-08-08
jquery序列化表單以及回調(diào)函數(shù)的使用示例
使用jQuery提供的表單序列化方法可以很好的解決JSP表單中一個個傳值的問題,下面有個示例,大家可以參考下2014-07-07
jQuery Validate 數(shù)組 全部驗證問題
這篇文章主要介紹了jquery validate 數(shù)組 全部驗證問題及解決辦法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-01-01
jQuery插件HighCharts繪制2D圓環(huán)圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts繪制2D圓環(huán)圖效果,結(jié)合實例形式分析了jQuery使用HighCharts插件繪制圓環(huán)圖的實現(xiàn)步驟與相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03

