zoukankan      html  css  js  c++  java
  • 用ajax获取后台数据,返回json数据,怎么在前台使用?

    用ajax获取后台数据,返回json数据,怎么在前台使用呢?

    后台

    C# code
     
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    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); }
                        }
     
                    


    前台

    JavaScript code
     
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     $(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"); }
                });
            })
            })



    参考代码

    JavaScript code
     
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    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"); }
                });
            })
            })
     
     
  • 相关阅读:
    如何用伪类画出一个三角形
    关于昨天遇到题目的一点随笔
    opacity与rgba
    选择框脚本_移动/重排选项 P435
    文字等宽
    CSS3风骚渐变
    表单序列化 P436
    选择框脚本_添加/删除选项 P434
    选择框脚本_用事件选中选项,获取选中项信息 P432
    文本框组脚本_自动切换焦点“例如加区号和分机号的电话号码文本框组” P426
  • 原文地址:https://www.cnblogs.com/aivnfjgj/p/6250215.html
Copyright © 2011-2022 走看看