麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

ajax的訪問WebService的方法

2019-11-14 16:00:33
字體:
來源:轉載
供稿:網友

如果想用Ajax進行訪問 首先在web.config里進行設置

添加在 

<webServices>
<PRotocols>
<add name= "HttpPost" />
<add name= "HttpGet" />
</protocols>
</webServices>

<system.web>節點之下

這樣就是可以通過url進行訪問了 不然就會報錯

 

然后直接亮代碼  代碼也是網上找到的  如有雷同,請聯系本人

 [WebService(Namespace = "http://tempuri.org/")]    [WebServiceBinding(ConformsTo = Wsiprofiles.BasicProfile1_1)]    [System.ComponentModel.ToolboxItem(false)]    // 若要允許使用 asp.net AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。    [System.Web.Script.Services.ScriptService]    public class WebService1 : System.Web.Services.WebService    {        /// <summary>        /// 無參數        /// </summary>        /// <returns></returns>        [WebMethod]        public string HelloWorld()        {            return "Hello World ";        }        /// <summary>        /// 帶參數        /// </summary>        /// <param name="value1"></param>        /// <param name="value2"></param>        /// <param name="value3"></param>        /// <param name="value4"></param>        /// <returns></returns>        [WebMethod]        public string GetWish(string value1, string value2, string value3, int value4)        {            return string.Format("祝您在{3}年里 {0}、{1}、{2}", value1, value2, value3, value4);        }        /// <summary>        /// 返回集合        /// </summary>        /// <param name="i"></param>        /// <returns></returns>        [WebMethod]        public List<int> GetArray(int i)        {            List<int> list = new List<int>();            while (i >= 0)            {                list.Add(i--);            }            return list;        }        /// <summary>        /// 返回一個復合類型        /// </summary>        /// <returns></returns>        [WebMethod]        public Class1 GetClass()        {            return new Class1 { ID = "1", Value = "牛年大吉" };        }        /// <summary>        /// 返回xml        /// </summary>        /// <returns></returns>        [WebMethod]        public DataSet GetDataSet()        {            DataSet ds = new DataSet();            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"] = "新年快樂";            dt.Rows.Add(dr);            dr = dt.NewRow();            dr["ID"] = "2";            dr["Value"] = "萬事如意";            dt.Rows.Add(dr);            ds.Tables.Add(dt);            return ds;        }    }    //自定義的類,只有兩個屬性    public class Class1    {        public string ID { get; set; }        public string Value { get; set; }    }

  然后是ajax的代碼

<script type="text/javascript">        //無參數調用        $(document).ready(function() {            $('#btn1').click(function() {                $.ajax({                    type: "POST",   //訪問WebService使用Post方式請求                    contentType: "application/json", //WebService 會返回Json類型                    url: "WebService1.asmx/HelloWorld", //調用WebService的地址和方法名稱組合 ---- WsURL/方法名                    data: "{}",         //這里是要傳遞的參數,格式為 data: "{paraName:paraValue}",下面將會看到                           dataType: 'json',                    success: function(result) {     //回調函數,result,返回值                        $('#dictionary').append(result.d);                    }                });            });        });        //有參數調用        $(document).ready(function() {            $("#btn2").click(function() {                $.ajax({                    type: "POST",                    contentType: "application/json",                    url: "WebService1.asmx/GetWish",                    data: "{value1:'心想事成',value2:'萬事如意',value3:'牛牛牛',value4:2009}",                    dataType: 'json',                    success: function(result) {                        $('#dictionary').append(result.d);                    }                });            });        });                        //返回集合(引用自網絡,很說明問題)        $(document).ready(function() {            $("#btn3").click(function() {                $.ajax({                    type: "POST",                    contentType: "application/json",                    url: "WebService1.asmx/GetArray",                    data: "{i:10}",                    dataType: 'json',                    success: function(result) {                        $(result.d).each(function() {                            //alert(this);                            $('#dictionary').append(this.toString() + " ");                            //alert(result.d.join(" | "));                        });                    }                });            });        });        //返回復合類型        $(document).ready(function() {            $('#btn4').click(function() {                $.ajax({                    type: "POST",                    contentType: "application/json",                    url: "WebService1.asmx/GetClass",                    data: "{}",                    dataType: 'json',                    success: function(result) {                        $(result.d).each(function() {                            //alert(this);                            $('#dictionary').append(this['ID'] + " " + this['Value']);                            //alert(result.d.join(" | "));                        });                    }                });            });        });        //返回DataSet(XML)        $(document).ready(function() {            $('#btn5').click(function() {                $.ajax({                    type: "POST",                    url: "WebService1.asmx/GetDataSet",                    data: "{}",                    dataType: 'xml', //返回的類型為XML ,和前面的Json,不一樣了                    success: function(result) {                    //演示一下捕獲                        try {                               $(result).find("Table1").each(function() {                                $('#dictionary').append($(this).find("ID").text() + " " + $(this).find("Value").text());                            });                        }                        catch (e) {                            alert(e);                            return;                        }                    },                    error: function(result, status) { //如果沒有上面的捕獲出錯會執行這里的回調函數                        if (status == 'error') {                            alert(status);                        }                    }                });            });        });        //Ajax 為用戶提供反饋,利用ajaxStart和ajaxStop 方法,演示ajax跟蹤相關事件的回調,他們兩個方法可以添加給jQuery對象在Ajax前后回調        //但對與Ajax的監控,本身是全局性的        $(document).ready(function() {            $('#loading').ajaxStart(function() {                $(this).show();            }).ajaxStop(function() {                $(this).hide();            });        });        // 鼠標移入移出效果,多個元素的時候,可以使用“,”隔開        $(document).ready(function() {            $('div.button').hover(function() {                $(this).addClass('hover');            }, function() {                $(this).removeClass('hover');            });        });                    </script>

  然后就是這5個ajax得到的響應

是不是很奇怪為什么json里都有個d  我也很奇怪估計是服務端把其他格式解析成json的時候自己添加的

還有一個問題 ajax的代碼  contentType: "application/json", //WebService 會返回Json類型

 dataType: 'json' 這2者的區別是啥  如果知道請告訴我下


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一级毛片在线免费播放 | 黄色电影免费网址 | 蜜桃免费在线 | 久久精品中文字幕一区二区三区 | 国产精品久久久久一区二区 | 精品国产96亚洲一区二区三区 | 中国产一级毛片 | 女人裸体让男人桶全过程 | 欧美精品久久久久久久多人混战 | 成人在线免费视频观看 | 黑人操穴 | 欧美黑人xx | 日本高清无遮挡 | 99精品视频久久精品视频 | 精品久久久久久久久久中出 | 99re久久最新地址获取 | 午夜视频国产 | 中文字幕在线免费看 | 99久久婷婷国产综合精品青牛牛 | 亚洲精品av在线 | 黄色特级片黄色特级片 | 色毛片 | 国产亚洲欧美日韩高清 | 欧美性生活久久 | 国产精品一区二区免费在线观看 | 日韩在线欧美在线 | 欧美一级免费视频 | 国产午夜精品理论片a级探花 | 欧美一级黄色录相 | 草逼一区 | 九九黄色 | 欧美hdfree性xxxx | 欧美一级在线免费 | 深夜影院一级毛片 | 日本黄色免费片 | 人人舔人人舔 | 欧美日韩一区,二区,三区,久久精品 | 久久久免费观看完整版 | 爱性久久久久久久 | 欧美成人精品一区二区 | 国产亚洲精品久久久久久网站 |