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;

ajax aspx调用webservice,返回json的更多相关文章

  1. ASP.net jQuery调用webservice返回json数据的一些问题

    之前寒假时,试着使用jQuery写了几个异步请求demo, 但是那样是使用的webform普通页面,一般应该是用 webservice 居多. 最近写后台管理时,想用异步来实现一些信息的展示和修改, ...

  2. jQuery调用WebService返回JSON数据

    相信大家都比较了解JSON格式的数据对于ajax的方便,不了解的可以从网上找一下这方面的资料来看一下,这里就不多说了,不清楚的可以在网上查一下,这里只说一下因为参数设置不当引起的取不到返回值的问题. ...

  3. 亲测 asp.net 调用 webservice返回json

    前端脚本 $("#sure").click(function () { var tbody = $("#putsigal tbody"); var trs = ...

  4. Asp.Net_Ajax调用WebService返回Json前台获取循环解析

    利用JQuery的$.ajax()可以很方便的调用 asp.net的后台方法.但往往从后台返回的json字符串不能够正确解析,究其原因,是因为没有对返回的json数据做进一步的加工.其实,这里只需 要 ...

  5. WebService返回json格式数据供苹果或者安卓程序调用

    1.新建一个WebService. 2. /// <summary> /// DemoToJson 的摘要说明 /// </summary> [WebService(Names ...

  6. jquery的ajax异步请求接收返回json数据

    http://www.jb51.net/article/51122.htm jquery的ajax异步请求接收返回json数据方法设置简单,一个是服务器处理程序是返回json数据,另一种就是ajax发 ...

  7. ajax请求、servlet返回json数据

    ajax请求.servlet返回json数据 1.方式一 response.setcontenttype("text/html;charset=utf-8"); response. ...

  8. ajax调用后台webservice返回JSON字符

    后台代码: [WebMethod] public static string LoginTest(string userCode, string password) { UserManageCente ...

  9. asp.net webservice返回json问题

    使用jQuery $.ajax方法请求webservice 一.方法返回值为string,将json格式的字符串返回 设置contentType为"application/json;char ...

随机推荐

  1. [翻译]NUnit---Action Attributes(八)

    Attributes NUnit 1.0使用传统的基于继承和命名约定来识别测试.从2.0开始NUnit使用自定义特性来实现. 因为NUnit的test fixtures不是从框架类库继承,所以开发人员 ...

  2. android 在代码中设置字体颜色 问题

    项目中需要在代码中控制字体颜色 之前是直接引用资源文件  但是不行 tv.setTextColor(R.color.textColor_black); 无效果   后来在网上找了资料发现 要从reso ...

  3. java 基础 —— 文件操作(File)

    1. 基本成员: File.separator public class File implements Serializable, Comparable<File> { private ...

  4. 0629-TP整理四(create(),success(),error(),U())

    create()-前提:表单中name的值要与数据库中的字段一一匹配 可直接获取表单数据进行操作: 作用:将数据库中没有的字段在数组中去除. PHP中添加的语法如下: success()和error( ...

  5. bzoj 1059: [ZJOI2007]矩阵游戏【匈牙利算法】

    注意到怎么换都行,但是如果把某个黑方块用在对角线上,它原来所在的行列的的黑方块就都不能用 所以要选出n组不重的行列组合,这里用匈牙利算法做二分图匹配即可(用了时间戳优化) #include<io ...

  6. 小记 vue 打包(build)需要注意的一些事

    记录 vue 项目打包的一些事情 首先声明项目都是由 vue-cli 生成; vue 项目从 dev 切换到 prod 时有很多地方需要注意; 首先是大家最需要注意的 ajax 切换环节 以前一开始用 ...

  7. golang——database/sql包学习

    1.database/sql包 sql包提供了保证SQL或类SQL数据库的泛用接口. 使用sql包时必须注入(至少)一个数据库驱动. (1)获取mysql driver:go get -v githu ...

  8. Office Excel的几个快捷键记录

    Office Excel的几个快捷键记录: 切换Sheet:CTRL + PageUP/PageDown 另存为:F12

  9. 全面学习ORACLE Scheduler特性(10)管理Chains

    5.2  管理Chains 5.2.1  修改Chains属性 基本上碰到修改CHAIN属性的机率不会太大,因此确实没啥可修改的,对于CHAIN对象来说,能够修改的属性只有两个:evaluation_ ...

  10. win10下spark+Python开发环境配置

    Step0:安装好Java ,jdk Step1:下载好: Step2: 将解压后的hadoop和spark设置好环境变量: 在系统path变量里面+: Step3: 使用pip安装 py4j : p ...