栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

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

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 += '' + ''; }); $("#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"); }
      });
    })
    })

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

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号

姓名电话
'; list += n.name + '' + n.company; list += '