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.mzph.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;各种类…