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

用ajax获取后台数据,返回json数据,怎么在前台使用?的更多相关文章

  1. ajax获取后台传递的json数据

      最近在使用JQuery的ajax方法时,需要返回的数据为json数据,在success返回中数据处理会根据返回方式不同会采用不同的方式来生成json数据.在$.ajax方法中应该是如何来处理的,简 ...

  2. ajax请求、servlet返回json数据

    ajax请求.servlet返回json数据 1.方式一 response.setcontenttype("text/html;charset=utf-8"); response. ...

  3. jquery的ajax异步请求接收返回json数据

    http://www.jb51.net/article/51122.htm jquery的ajax异步请求接收返回json数据方法设置简单,一个是服务器处理程序是返回json数据,另一种就是ajax发 ...

  4. ajax请求后台,返回json格式数据,模板!

    添加一个用户的时候,需要找出公司下所有的部门,和相应部门下的角色,利用ajax请求,实现联动技术.将返回的json格式数据,添加到select标签下. <script type="te ...

  5. 使用jQuery解析JSON数据(由ajax发送请求到php文件处理数据返回json数据,然后解析json写入html中呈现)

    在上一篇的Struts2之ajax初析中,我们得到了comments对象的JSON数据,在本篇中,我们将使用jQuery进行数据解析. 我们先以解析上例中的comments对象的JSON数据为例,然后 ...

  6. 如何用 ajax 连接mysql数据库,并且获取从中返回的数据。ajax获取从mysql返回的数据。responseXML分别输出不同数据的方法。

    开讲前,先说下网上,大部分的关于这方面的博文或者其他什么的,就我自己的感觉,第一说得不详细,第二语言不能很好的被初学者了解. 我这篇博文的标题之所以用了三句,是为了方便其他人好查找: 这里介绍的方法有 ...

  7. 用jQuery的ajax请求一般处理程序返回json数据

    1.web页面代码: 注意事项: dataType类型一定要写成json. 2.一般处理程序代码: 注意事项: ContentType类型写成"application/json"或 ...

  8. ajax调用后台webservice返回JSON字符

    后台代码: [WebMethod] public static string LoginTest(string userCode, string password) { UserManageCente ...

  9. Php如何返回json数据,前后端分离的基本解决方案

    php返回json,xml,JSONP等格式的数据 返回json数据: header('Content-Type:application/json; charset=utf-8'); $arr = a ...

  10. Php如何返回json数据

    php返回json,xml,JSONP等格式的数据 返回json数据: header('Content-Type:application/json; charset=utf-8'); $arr = a ...

随机推荐

  1. http://blog.csdn.net/shawnkong/article/details/52045894

    http://blog.csdn.net/shawnkong/article/details/52045894

  2. make[2]: *** No rule to make target `/root/.pyenv/versions/anaconda3-2.4.0/lib/libpython3.5m.so', needed by `evaluation.so'. Stop.

    当出现No rule to make target ,肯定是Makefile有问题. 有的makefile是脚本生成的,你得看脚本的配置文件对不对. 我的是这个脚本生成的.发现是Pythondir的配 ...

  3. android subclipse subversive

    subclipse - http://subclipse.tigris.org/update_1.10.x android 开源框架 直接拿来用!最火的Android开源项目整理 http://blo ...

  4. ng-style 的坑 - 对性能的影响

    本文地址:http://www.cnblogs.com/jying/p/5633203.html 熟悉 angular 的前端对ng-style 一定不陌生,这个家伙可以绑定一个函数,使得我们可以在函 ...

  5. 查看表的datapages

    select object_name(i.object_id) as tableName,data_pages as dataPagesfrom sys.indexes as ijoin sys.pa ...

  6. 学习C:程序

    #include <stdio.h> main(){ /*将输入复制到输出,并将其中连续的多个空格用一个空格代替*/ int c,nb; nb = 0; while((c = getcha ...

  7. vs2012启动网站调试提示 HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容

    启动vs2012调试网站的时候提示: HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容. 最可能的原因: 没有为请求的 URL 配置默认文档,并且没有在服 ...

  8. python之路——面向对象(进阶篇)

    面向对象进阶:类成员.类成员的修饰符.类的特殊成员 类成员 类成员分为三大类:字段.方法.属性 一.字段 静态字段 (属于类) 普通字段(属于对象) class City: # 静态字段 countr ...

  9. Windows自带.NET Framework版本大全

    The following is a complete list of which version of the .NET Framework is included in which version ...

  10. Webstorm官方最新版本for Mac版本 不用注册码/破坏原文件

    首先,说明下我自己安装的时候看到网上无外乎两种方法: 下载别人封装好的安装包,把JetbrainsCrack.jar复制到/Applications/WebStorm.app/Contents/bin ...