ajax与json 获取数据并在前台使用简单实例
2017-01-19 14:58
966 查看
用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 = '<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数据是列表还是单个了,就一条就无需中括号了
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使用Action中返回的Json数据的简单实例(一)
- 前台使用$.ajax()发送json数据,服务器响应后返回json数据实例(struts2+json+action)
- jQuery使用ajax跨域获取数据的简单实例
- jQuery使用ajax跨域获取数据的简单实例
- 使用所见即所得文本编辑器编辑文本存入数据库后通过ajax获取服务器json_encode的数据到前台,文本内容上边的html标签不解析
- ASP.NET AJAX中 复杂数据类型使用简单实例
- MVC中使用ajax和json传递数据简单示例
- 如何在前台脚本通过json传递数据到后台(使用微软自带的ajax)
- .Net MVC3之1:前台异步获取json数据的实例(1)
- jQuery中使用Ajax获取JSON格式数据示例代码
- jQuery中使用Ajax获取JSON格式数据示例代码
- Android使用Json获取数据实例【这个例子不太好,仅作参考】
- JSP中获取ExtJS.Ajax前台传递的JSON数据
- AJAX 跨域请求的解决办法:使用 JSONP获取JSON数据
- 如何在前台脚本通过json传递数据到后台(使用微软自带的ajax)
- Jquery 使用Ajax获取后台返回的Json数据后,页面处理
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及非文件上传的部分
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及文件上传的部分
- JSP中获取ExtJS.Ajax前台传递的JSON数据实现过程
- JQUERY 实现AJAX跨域获取json数据实例