用ajax获取后台数据,返回json数据,怎么在前台使用呢?
创新互联建站 2013年至今,先为柏乡等服务建站,柏乡等地企业,进行企业商务咨询服务。为柏乡企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
后台
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"); }
});
})
})
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
网页标题:ajax与json获取数据并在前台使用简单实例
链接分享:http://www.ncjierui.cn/article/gcpsso.html
10年专业互联网服务经验
彭山高端建站设计团队
资深行业分析策划
B2C营销型网站建设者
前沿视觉设计、研发能力
前端代码深度符合SEO优化
彭山市高新技术企业证书
具有完备的项目管理
完善的售后服务体系
深厚的网络运营经验
时刻新技术研发能力
擅长网站设计营销推广
关于美图云海网站建设更多+
美图云海网站设计,为客户量身定制各类网站建设业务,包括企业型、电子商务型、行业门户型、品牌建立型等各类网站,实战经验丰富,成功案例众多。以客户利益为出发点,美图云海网站建设网站制作为客户规划、定制符合企业需求、带有营销价值的建站方案,提供从网站前期定位分析策划到网站界面设计...