C# WebService输出JSON 实现二
一般js请求web服务uk可以通过 contentType: "application/json" 获取json效果,为了取得更好的效果,可以在服务端强制返回JSON格式
服务端代码(c#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using Newtonsoft.Json; namespace ajaxjson
{
/// <summary>
/// demo 的摘要说明
/// </summary>
[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 demo : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
} [WebMethod]
public void Login(string username,string password)
{
User u=new User();
u.name = "demo";
u.username = username;
u.password = password;
u.money = 1.00;
string json = JsonConvert.SerializeObject(u);
Context.Response.Write(json);
Context.Response.End();
}
[WebMethod]
public void GetUser(int id)
{
User u = new User();
u.name = "demo";
u.username = "ddd";
u.password = "";
u.money = 1.00;
string json = JsonConvert.SerializeObject(u);
Context.Response.Write(json);
Context.Response.End();
} [WebMethod]
public void GetList()
{
List<User> list=new List<User>();
User u = new User();
u.name = "demo";
u.username = "";
u.password = "";
u.money = 1.00;
list.Add(u); u = new User();
u.name = "demo";
u.username = "";
u.password = "";
list.Add(u);
//该处理会导致必须使用json处理
string json = JsonConvert.SerializeObject(list);
Context.Response.Write(json);
Context.Response.End();
}
} public class User
{
public string username { get; set; }
public string password { get; set; }
public string name { get; set; }
public double money { get; set; }
}
}
客户端代码
jQuery.ajax( {
url:'http://127.0.0.1:81/demo.asmx/Login',
data:{
username:"123456",
password:"123456"
},
type:'post',
dataType:'json',
success:function(data) {
//===========
},
error : function(xhr,status,error) {
//===========
}
});
补充AJAX跨域问题:
由于JSONP get的限制这里采用XHR2的CORS的模式,即利用Headers中的Origin参数实现,这里直接在配置文件中设置,留意红色部分
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime/>
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
</protocols>
</webServices>
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
<add name="Access-Control-Allow-Headers" value="Origin,X-Requested-With,Content-Type,Accept" />
<add name="Access-Control-Allow-Origin" value="*"/>
</customHeaders>
</httpProtocol>
</system.webServer>
<!--配置JSON序列化-->
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength=""/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
配置后就可以使用普通的AJAX方式访问该服务了
扩展:PHP中跨域配置
header('Access-Control-Allow-Origin: *');
//header('Content-type: text/plain');
C# WebService输出JSON 实现二的更多相关文章
- python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码
python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...
- 简单实体Json序列化(输出JSON的属性可变)
简单实体Json序列化(输出JSON的属性可变) 一.先看效果 可以看出 , 我们在序列化一个对像时, 只给出了 我们想要 输出的两个字段名, 实际实体有5个属性, 经过可变属性序列化后的JSON ...
- WebService使用JSON格式传递笔记+JQuery测试
原文WebService使用JSON格式传递笔记+JQuery测试 因为一些因素,必须改写WebService,很传统,但是很多公司还在用.. 因为XML 的关系,不想让他传递数据的时候过度肥大,所以 ...
- Golang 处理 Json(二):解码
golang 编码 json 还比较简单,而解析 json 则非常蛋疼.不像 PHP 一句 json_decode() 就能搞定.之前项目开发中,为了兼容不同客户端的需求,请求的 content-ty ...
- PHP、Java输出json格式数据
PHP 输出json. $result = mysql_query($sql); //查询结果 $users=array(); $i=0; while($row=mysql_fetch_array ...
- 为ASP.NET MVC视图输出json
做个小小练习,为asp.net mvc视图输出json字符串: 创建JsonResult操作: 创建此视图: 浏览结果:
- Spring Mvc 输出Json(iwantmoon.com出品)
原文:http://iwantmoon.com/Post/f94e49caf9b6455db7158474bab4c4dd 因为工作需要,现在要去做开放平台,考虑了多种方案之后,基本确定 下来,Htt ...
- C#开发的WebService使用JSON格式传递数据+Ajax测试
[C#] WebService 使用 JSON 格式傳遞筆記 + JQuery 測試 0 2 因為一些因素,必須改寫WebService,很傳統,但是很多公司還在用.. 因為XML 的關係,不想讓他 ...
- 在JSP页面中输出JSON格式数据
JSON-taglib是一套使在JSP页面中输出JSON格式数据的标签库. JSON-taglib主页: http://json-taglib.sourceforge.net/index.html J ...
随机推荐
- Forward-backward梯度求导(tensorflow word2vec实例)
考虑不可分的例子 通过使用basis functions 使得不可分的线性模型变成可分的非线性模型 最常用的就是写出一个目标函数 并且使用梯度下降法 来计算 梯度的下降法的梯度 ...
- 事务操作(BEGIN/COMMIT/ROLLBACK/SAVE TRANSACTION)
BEGIN TRANSACTION 标记一个显式本地事务的起始点. BEGIN TRANSACTION 使 @@TRANCOUNT 按 1 递增. BEGIN TRANSACTION 代表一点,由连接 ...
- [转]Ubuntu 12.04中文输入法的安装
Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架. 在Ubuntu的中文系统中自带了中文输入法,通过Ctrl+S ...
- IIS服务器允许跨域配置
<configuration> <system.webServer> <httpProtocol> <customHeaders> <add na ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- jvm中的年轻代 老年代 持久代 gc
虚拟机中的共划分为三个代:年轻代(Young Generation).老年代(Old Generation)和持久代(Permanent Generation).其中持久代主要存放的是Java类的类信 ...
- win7 eclipse 调试storm
windows 下eclipse开发storm 用本地模式,直接run as 运行topology解决了可以什么都不用下,直接把storm-starter的源码下下来,1.在eclipse创建一个ja ...
- T-SQL Recipes之生成动态列表数据
Problem 首先什么是动态列表?举个示例,假设你想输出以逗号分隔的IDs,如: 1,45,67,199,298 Solution 生成动态列表数据在我们生活场景中很常见,比如在 Dynamic P ...
- 理解margin
margin可以改变容器的尺寸 //元素尺寸分为可视尺寸,占据尺寸 margin与可视尺寸 1.适用于没有设定width/height的普通block水平元素 2.只适用于水平方向的尺寸 应用:一侧定 ...
- CSS Sprites+CSS3 Icon Font
CSS Sprites+CSS3 Icon Font CSS Sprites在国内很多人叫CSS精灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来, ...