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. 操作系统学习笔记:CPU调度

    CPU调度的目的在于提高CPU利用率,不让CPU闲着.CPU是宝贵的资源,如果有一个进程,本来在CPU中运行,忽然因为要使用IO资源,于是转而请求IO,这边CPU挂起,造成就绪队列中的其他进程等待,这 ...

  2. servlet container:tomcat jetty and undertow

    1 spring boot内嵌容器支持tomcat.jetty和undertow 但是undertow性能最好,详见: https://examples.javacodegeeks.com/enter ...

  3. android短信拦截

    广播分2种,无序广播和有序广播.可以理解为散列和队列广播. 首先无序广播,不能中断,分发机制有点类似散列发送.这种广播的的发送为:context.sendBroadcast这种广播是不能中断的,请看A ...

  4. linux怎么返回上级目录啊,用cd/命令却这样:bash:cd/:没有那个文件或目录

    多打一个空格键盘又不会坏.cd 空格 .. 是上一级cd 空格 / 是回最高级,也就是 / 相应的cd 空格 ../../abc 就是去上级目录的上级目录里面的 abc 目录里.Linux 里面,所有 ...

  5. A brief preview of the new features introduced by OpenGL 3.3 and 4.0

    A brief preview of the new features introduced by OpenGL 3.3 and 4.0   The Khronos Group continues t ...

  6. bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】

    这个题方法还挺多的,不过洛谷上要输出方案所以用堆最方便 先按起始时间从小到大排序. 我用的是greater重定义优先队列(小根堆).用pair存牛棚用完时间(first)和牛棚编号(second),每 ...

  7. bzoj 1634: [Usaco2007 Jan]Protecting the Flowers 护花【贪心】

    因为交换相邻两头牛对其他牛没有影响,所以可以通过交换相邻两头来使答案变小.按照a.t*b.f排降序,模拟着计算答案 #include<iostream> #include<cstdi ...

  8. php 批量检测bom头,去除bom头工具

    <?php //有些php文件由于不小心保存成了含bom头的格式而导致出现一系列的问题.以下是批量清除bom头的代码 if (isset ( $_GET ['dir'] )) { //confi ...

  9. JavaScript--userAgent

    userAgent 返回用户代理头的字符串表示(就是包括浏览器版本信息等的字符串) 语法 navigator.userAgent 几种浏览的user_agent.,像360的兼容模式用的是IE.极速模 ...

  10. C#与C++的区别(三) 委托与事件

    在C#中没有C++中的函数指针的概念,但是有委托的概念,功能与函数指针类似. C# 委托(Delegate) C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针.委托(Delega ...