ajax与json 获取数据并在前台使用简单实例
发布于 2017-05-18 14:05:38 | 63 次阅读 | 评论: 0 | 来源: 网友投递
AjaxAJAX即“Asynchronous Javascript + XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。
这篇文章主要介绍了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"); }
});
})
})
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关阅读:
ajax与json 获取数据并在前台使用简单实例
原生ajax处理json格式数据的实例代码
PHP 利用AJAX获取网页并输出的实现代码
Ajax与服务器(JSON)通信实例代码
使用Ajax与服务器(JSON)通信实例
基于ajax实现文件上传并显示进度条
ajax与302响应代码测试
ajax中get和post的说明及使用与区别
基于ajax与msmq技术的消息推送功能实现代码
使用Ajax生成的Excel文件并下载的实例
有关文件上传 非ajax提交 得到后台数据问题
php+ajax简单实例代码