ajax的訪問 WebService 的方法

轉自原文 ajax的訪問 WebService 的方法

如果想用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者的區別是啥 ?如果知道請告訴我下。

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540610.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540610.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540610.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[轉載] 使用DirectInput進行交互

參考鏈接&#xff1a; input()函數中的漏洞– Python2.x 使用DirectInput進行交互&#xff08;1&#xff09; DirectX 2008-08-10 15:11:34 閱讀169 評論0 字號&#xff1a;大 中 小 訂閱 輸入設備簡介 計算機通常使用三種輸入設備&#xff1a;鍵盤、鼠標和游…

c語言 nan 常量_NaN32常量(Julia)

c語言 nan 常量Julia| NaN32常數 (Julia | NaN32 Constant) NaN32 is a constant of the Float32 type in Julia programming language, it represents "not-a-number" value. NaN32是Julia編程語言中Float32類型的常量&#xff0c;它表示“非數字”值。 Syntax: 句…

Hyperledger Fabric 1.0 從零開始(七)——啟動Fabric多節點集群

5&#xff1a;啟動Fabric多節點集群 5.1、啟動orderer節點服務 上述操作完成后&#xff0c;此時各節點的compose配置文件及證書驗證目錄都已經準備完成&#xff0c;可以開始嘗試啟動多機Fabric集群。 首先啟動orderer節點&#xff0c;切換至orderer.example.com服務器&#xff…

[轉載] python中print()函數的用法和end=““不換行詳解

參考鏈接&#xff1a; Python | print()中的結束參數 需求&#xff1a;打印五個字符&#xff0c;在一行上 代碼&#xff1a; i 0 while i< 5 : i 1 print(i,end’’) 結果&#xff1a; 1 2 3 4 5那么問題來了&#xff0c;為什么加一個end"" 就不換…

css中圖片左右邊距_CSS中的邊距

css中圖片左右邊距CSS保證金屬性 (CSS margin property) CSS Margins are used to space around any element, for this we use "margin" property in the CSS. CSS邊距用于在任何元素之間留出空間&#xff0c;為此&#xff0c;我們在CSS中使用“ margin”屬性 。 S…

js 實現網頁顯示倒計時

用 js 來實現網頁顯示倒計時效果 1 function checkTime( time ){2 var data new Data(); // 獲取現在時間3 var nowData data.getTime(); // 轉化成毫秒數4 var time ; // 結束的時間5 var t time - nowData ;6 var HH, mm , ss 0;7 var sta "…

scala方法中的變量_Scala中的變量

scala方法中的變量Scala變量 (Scala variables) A variable is named a reference to a memory location. The location stores the data that is used by the program. 變量被稱為對存儲位置的引用。 該位置存儲程序使用的數據。 Based on the data type of the variable the…

[轉載] python[1]-print中的sep、end參數

參考鏈接&#xff1a; Python | print()中的sep參數 讀示例程序代碼時遇到的問題&#xff0c;看不懂end和sep參數。經過查找&#xff0c;基本弄清楚了。 sep&#xff1a;可以設置print中分割不同值的形式。應該是separation的縮寫。 end&#xff1a;可以設置print打印結束時最…

分區 主分區 和 擴展分區_等和分區

分區 主分區 和 擴展分區Description: 描述&#xff1a; This is a popular interview coding problem which has been featured in interview rounds of Amazon, Oyo rooms, Adobe. 這是一個受歡迎的采訪編碼問題&#xff0c;已在亞馬遜&#xff0c;Oyo房間&#xff0c;Adobe…

ORACLE 物理讀 邏輯讀 一致性讀 當前模式讀總結淺析

在ORACLE數據庫中有物理讀&#xff08;Physical Reads&#xff09;、邏輯讀&#xff08;Logical Reads&#xff09;、一致性讀&#xff08;Consistant Get&#xff09;、當前模式讀&#xff08;DB Block Gets&#xff09;等諸多概念&#xff0c;如果不理解或混淆這些概念的話&a…

[轉載] Java Formatter toString()方法與示例

參考鏈接&#xff1a; Python | 輸出格式化 output format 格式化程序類toString()方法 (Formatter Class toString() method) toString() method is available in java.util package. toString()方法在java.util包中可用。 toString() method is for the string representat…

arm tbh_TBH的完整形式是什么?

arm tbhTBH&#xff1a;說實話 (TBH: To Be Honest) TBH is an abbreviation of "To Be Honest". It is internet slang which generally used as an acronym or hashtag over the internet on social media networking sites like Facebook, Instagram, Twitter, Yo…

異常:fatal: unable to access 'https://git.oschina.net/pcmpcs/library.git/': Could not resolve host...

git fork項目時出現的異常. 原因: 我以前用的是ssh地址做的遠程通信地址&#xff0c;而這次是用的是https&#xff0c;因為很久沒用&#xff0c;所以忘記了以前是用ssh的了。解決方案一&#xff1a;復制ssh協議的地址&#xff0c;然后再關聯遠程倉庫。并且在VCS下的git下的Rem…

計數器數組_子數組計數

計數器數組Problem statement: 問題陳述&#xff1a; Given an array of N positive integers a1, a2, ..., an. The value of each contiguous subarray of a given array is the maximum element present in that subarray. The task is to return the number of subarrays…

[轉載] 列表、元組及通用序列操作

參考鏈接&#xff1a; Python | 重點數據類型 (字符串&#xff0c;列表&#xff0c;元組&#xff0c;迭代)(String, List, Tuple, Iteration) 序列是Python中最基本的數據結構&#xff08;一些基本特性類似于C中的數組模板類&#xff09;&#xff0c;序列中的每一個元素都有相…

onActivityResult()后onresume()

當你調用完一個存在的activity之后&#xff0c;onActivityResult將會返回以下數據&#xff1a;你調用時發出的requestCode、被調用activity的結果標志resultCode&#xff08;如RESULT_OK&#xff09;和其他的額外數據。我們期望的都是得到RESULT_OK&#xff0c;表示調用成功&am…

java反射用法示例_Java包| 類型,用法,示例

java反射用法示例配套 (Packages) Packages in Java is simply a mechanism to encapsulate (i.e. to put in a short and concise form) a group of classes,interfaces,enumerations, sub packages, etc. In real world, application is developed in such a manner so that …

[轉載] python 元組tuple - python基礎入門(14)

參考鏈接&#xff1a; Python元組Tuple 目錄 一.元組tuple定義 二.元組tuple查詢 三.元組tuple不支持刪除/修改數據 四.元組tuple與列表list的相互轉換 五.重點總結 在上一篇文章中我們講解了關于python列表List的相關內容&#xff0c;今天給大家解釋一下列表List的…

MaxCompute 2.0—從ODPS到MaxCompute

從ODPS到MaxCompute-阿里大數據的進化之路是一個商用大數據系統發展史&#xff0c;一個商業大數據系統要解決的問題有可靠性&#xff0c;高性能&#xff0c;安全性等等六個方面。內部產品名ODPS的MaxCompute&#xff0c;是阿里巴巴內部發展的一個高效能、低成本&#xff0c;完全…

python數值類型_Python數值類型

python數值類型In programming, Data Types are an essential concept. Data of various types can be stored in variables as per the task we want the variables to perform. 在編程中&#xff0c;數據類型是必不可少的概念。 根據我們希望變量執行的任務&#xff0c;各種類…