ajax前端怎么用,ajax与json 获取数据并在前台使用简单实例

用ajax获取后台数据,返回json数据,怎么在前台使用呢?

后台

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', //接受数据格式

data: 'DataType=SearchCustomer&CustomerID=' + document.getElementById("Text3").value, //要传递的数据

success:SucCallback,

error: function () { alert("error"); }

});

})

})

参考代码

grid.getCustomer(1,2,function (data) {

var list = '

' + tree_GridInfo._name + '的用户有


';

list += '

姓名电话

$.each(data, function (i, n) {

list += '

';

list += n.name + '

' + '' + n.company;

list += '

';

});

$("#SearchResult").html(list)

看你的json数据是列表还是单个了,就一条就无需中括号了

context.Response.Write("{\"name\":无用户,\"id\":\"0\",\"company\":\"无用户\"}");

$(document).ready(function () {

$("#Button3").click(

function (SucCallback) {

$.ajax(

{

type: "get",

url: 'GridDatas.ashx', //后台处理程序

dataType: 'json', //接受数据格式

data: 'DataType=SearchCustomer&CustomerID=' + document.getElementById("Text3").value, //要传递的数据

function (dataJson) {

alert(dataJson.Name);

alert(dataJson.Id);

},

error: function () { alert("error"); }

});

})

})

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值