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"); }
});
})
})

参考代码


'; list += '姓名

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

$(document).ready(function () {
$("#Button3").click(
function (SucCallback) {
$.ajax(
{
type: "get",//要传递的数据
function (dataJson) {
alert(dataJson.Name);
alert(dataJson.Id);
},error: function () { alert("error"); }
});
})
})

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

相关文章

$.AJAX()方法中的PROCESSDATA参数 在使用jQuery的$.ajax()方...
form表单提交的几种方式 表单提交方式一:直接利用form表单提...
文章浏览阅读1.3k次。AJAX的无刷新机制使得在注册系统中对于...
文章浏览阅读1.2k次。 本文将解释如何使用AJAX和JSON分析器在...
文章浏览阅读2.2k次。/************************** 创建XML...
文章浏览阅读3.7k次。在ajax应用中,通常一个页面要同时发送...
电话