zoukankan      html  css  js  c++  java
  • ajax

    jquery ajax call webservice: return json

    View Code
    <script type="text/javascript">
        $(function () {
    
            $("#btn").click(function () {
    
                $.ajax({
                    type: 'POST',
                    dataType: "json",
                    contentType: 'application/json; charset=utf-8',
                    url: '@Url.Content("~/services/WebService1.asmx/search")',
                    data: "{ 'id': '1' }",
                    success: function (data, textStatus, xhr) {
                        var rows = $.map(jQuery.parseJSON(data.d), function (item) {
                            return ('<tr><td>' + item.name + '</td></tr>');
                        }).join('');
                        $("#searchresults").html("<table>"+rows+"</table>");
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        alert("erropr");
                    }
                });
            });
    
        });
    </script>

    webservice return:

                return "[{\"name\":\"this is name\",\"value\":\"This is value\"},{\"name\":\"this is name2\",\"value\":\"This is value2\"}]";

    //if webservice return list<T> then use data,not data.d

    jquery ajax call webservice . return xml

    View Code
    <script type="text/javascript">
        $(function () {
    
            $("#btn").click(function () {
    
                $.ajax({
                    type: 'POST',
                    dataType: "xml",
                    contentType: 'application/json; charset=utf-8',
                    url: '@Url.Content("~/services/WebService1.asmx/getDS")',
                    data: "{ 'id': '1' }",
                    success: function (data, textStatus, xhr) {
                        try {
                            $(xhr.response).find("Table1").each(function () {
                                $('#searchresults').append($(this).find("ID").text() + " <-->" + $(this).find("Value").text());
                            });
                        }
                        catch (e) {
                            alert(e);
                            return;
                        }
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        alert("erropr");
                    }
                });
            });
    
        });
    </script>

    webservice return:

    View Code
    [WebMethod]
            [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Xml)]
            public DataSet getDS(string id)
            {
                DataSet ds = new DataSet("table");
                DataTable dt = new DataTable();
                dt.Columns.Add("ID", Type.GetType("System.String"));
                dt.Columns.Add("Value", Type.GetType("System.String"));
                DataRow dr = dt.NewRow();
                dr["ID"] = "1";
                dr["Value"] = "你好1";
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr["ID"] = "2";
                dr["Value"] = "新年好";
                dt.Rows.Add(dr);
                ds.Tables.Add(dt);
                return ds;
    
            }

    jquery ajax call ashx:

    View Code
    <script type="text/javascript">
        $(function () {
    
            $("#btn").click(function () {
    
                $.ajax({
                    type: 'POST',
                    dataType: "json",
                    contentType: 'application/json; charset=utf-8',
                    url: '@Url.Content("~/services/WebService1.asmx/search")',
                    data: "{ 'id': '1' }",
                    success: function (data, textStatus, xhr) {
                        var rows = $.map(data, function (item) {
                            return ('<tr><td>' + item.name + '</td></tr>');
                        }).join('');
                        $("#searchresults").html("<table>"+rows+"</table>");
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        alert("erropr");
                    }
                });
            });
    
        });
    </script>

    ashx return:

    View Code
     public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
    
                JavaScriptSerializer json = new JavaScriptSerializer();
                string value = json.Serialize(new Person { name = "aaa", age = "bbb" });
    
    
                //context.Response.Write(value);
                //context.Response.Write("{'name': 'firstname', 'value': 'Hello'}");
                context.Response.Write("[{\"name\":\"this is name\",\"value\":\"This is value\"},{\"name\":\"this is name2\",\"value\":\"This is value2\"}]");
                context.Response.End();
            }
  • 相关阅读:
    react深入学习(资料,案例)
    match.exec深入学习
    实用插件表格行列隐藏显示
    下拉选项插件的实现
    表格操作eventTable
    [CentOS7] 挂载iso镜像文件到/media目录下
    [CentOS7] 设置开机启动方式(图形界面或命令行)
    [CentOS7] 磁盘分区(gdisk, fdisk)
    [CentOS7] minimal安装后 出现 没有ifconfig 无法ping 无法yum could not retrieve mirrorlist http://mirrorlist.centos.org/
    [C++]C,C++中使用可变参数
  • 原文地址:https://www.cnblogs.com/yk00/p/2969035.html
Copyright © 2011-2022 走看看