1,创建一个asp.net网站

2.创建一个student类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
/// Student 的摘要说明
/// </summary>
public class Student
{
public Student()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public int Id { get; set; }
public String Name { get; set; }
public int Age { get; set; } }
 3,创建一个webservice;(添加引用-web服务)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services; /// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService { public WebService () { //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} [WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)] //ajax调用时加上这句话
public List<Student> getStudents() {
//Student s = new Student() {Id= 1,Name= "sa",Age= 18 };
List<Student> list = new List<Student>() {
new Student(){ Id= ,Name= "zhangsna",Age= },
new Student(){Id=,Name="lisi",Age=},
new Student(){Id =,Name="hehe",Age=}
};
return list;
}
[WebMethod]
public Student getStudent()
{
Student s = new Student() {Id= ,Name= "sa",Age= }; return s;
}
}

4,创建一个html页面,ajax调用

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="../Scripts/jquery-1.8.2.min.js"></script>
<script src="../Scripts/jquery-1.8.2.intellisense.js"></script>
<script type="text/javascript">
$(function () {
//1,请求一个字符串hello world
//$.ajax({
// url: "/WebService.asmx/HelloWorld",
// datatype:
//});
//$.post(
// "/WebService.asmx/HelloWorld",
// {},
// function (data) {
// alert($(data).text());
// }
// ); //2,请求getStudents方法
$.ajax({
type: "POST",
contentType: "application/json; charset=utf8",
url: "/WebService.asmx/getStudents",
dataType: "json",
success: successCallback,
error:errorCallback
});
function successCallback(data) {
for (var i = ; i < data.d.length; i++) {
alert(data.d[i].Name + data.d[i].__type);
}
}
function errorCallback(XMLHttpRequest, status, errorThrown) {
alert(status + errorThrown);
}
//3,$.post $.get请求,没有成功;原因contengtype无法设置
//$.post(
// "/WebService.asmx/getStudents",
// { "contentType": "application/json; charset=utf-8" },
// function (data, status, jqxht) {
// alert(jqxht);
// alert(status);
// for (var i = 0; i < data.d.length; i++) {
// alert(data.d[i].Name);
// }
// }, "script"
//);
});
</script>
</head>
<body> </body>
</html>

5,创建一个aspx页面,调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServiceReference;
using t74webservice;
public partial class webservice_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
t74webservice.WebServiceSoapClient ws = new t74webservice.WebServiceSoapClient();
//ws.HelloWorld()
t74webservice.Student s=ws.getStudent();
Response.Write(s.Age);
}
}

ok,以上是今天所学内容,两种调用webservice方法;

总结:1,ajax调用:

    a,方法加上特性[scriptMethod(ResponseFormat=ResponseFormat.Json,UserHttpGet=true)]   //true/flase get请求

    (需要引用命名空间 system.Web.Script.Services;);

    b,只能使用$.ajax调用,设置contentType:"application/json;charset=utf-8";

      $.post与$.get不能调用的原因是:无法设置contentType,使其请求为json格式;   待以后处理----

    c,前台使用时:data.d[i].Name; 原因-->返回的json为:{“d”:[{},{}]};

      

function successCallback(data) {
for (var i = ; i < data.d.length; i++) {
alert(data.d[i].Name + data.d[i].__type);
}
}

2,aspx 调用

引用web服务---》输入地址(或发现解决方案服务)

引用命名空间---new----调用方法---ok;

最新文章

  1. C/C++编译和链接过程详解 (重定向表,导出符号表,未解决符号表)
  2. slidedoor滑动门特效
  3. Spark环境搭建遇到的问题
  4. Robot Framework安装教程
  5. 微信第三方登陆,无需注册一键登录,获取用户信息,PHP实现方法
  6. FZU 2141 Sub-Bipartite Graph
  7. Node.js superagent 采集 URL 编码问题
  8. jmeter随笔(2)--上传接口报错
  9. 深入Mysql 导入导出
  10. UOJ 52 元旦激光炮
  11. python 笔记4-- 函数式编程
  12. EL字符串表达式和常用功能用途拦截
  13. BAPI_GOODSMVT_CREATE 移动类型311 CODE = &#39;04&#39; 代码
  14. 用Hashcat每秒计算1.4亿个密码,破解隔壁WIFI密码
  15. phpmyadmin的初始账号密码是多少
  16. java web(一):tomcat服务器的安装和简单介绍,与eclipse关联
  17. 移动端响应式布局--你不知道的CSS3.0媒体查询,解决rem部分情况下无法适配的场景
  18. Authorization Security for Mongodb
  19. MongoDB学习小结
  20. &lt;aop:aspect&gt;与&lt;aop:advisor&gt;的区别

热门文章

  1. Java:String和Date、Timestamp之间的转换【转】
  2. cojs 1001. [WZOI2011 S3] 消息传递
  3. OSS与文件系统的对比
  4. Ubuntu利用TCP协议来获取server时间
  5. Bootstrap popover源码分析
  6. 关于Time Series Database
  7. android获取手机的IMSI码
  8. bzoj3786
  9. 【168】ENVI入门系列
  10. 【转载】存储的一些基本概念(HBA,LUN)