一般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 实现二的更多相关文章

  1. python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码

    python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...

  2. 简单实体Json序列化(输出JSON的属性可变)

    简单实体Json序列化(输出JSON的属性可变) 一.先看效果 可以看出 , 我们在序列化一个对像时, 只给出了 我们想要 输出的两个字段名,  实际实体有5个属性, 经过可变属性序列化后的JSON ...

  3. WebService使用JSON格式传递笔记+JQuery测试

    原文WebService使用JSON格式传递笔记+JQuery测试 因为一些因素,必须改写WebService,很传统,但是很多公司还在用.. 因为XML 的关系,不想让他传递数据的时候过度肥大,所以 ...

  4. Golang 处理 Json(二):解码

    golang 编码 json 还比较简单,而解析 json 则非常蛋疼.不像 PHP 一句 json_decode() 就能搞定.之前项目开发中,为了兼容不同客户端的需求,请求的 content-ty ...

  5. PHP、Java输出json格式数据

      PHP 输出json. $result = mysql_query($sql); //查询结果 $users=array(); $i=0; while($row=mysql_fetch_array ...

  6. 为ASP.NET MVC视图输出json

    做个小小练习,为asp.net mvc视图输出json字符串: 创建JsonResult操作: 创建此视图: 浏览结果:

  7. Spring Mvc 输出Json(iwantmoon.com出品)

    原文:http://iwantmoon.com/Post/f94e49caf9b6455db7158474bab4c4dd 因为工作需要,现在要去做开放平台,考虑了多种方案之后,基本确定 下来,Htt ...

  8. C#开发的WebService使用JSON格式传递数据+Ajax测试

    [C#]  WebService 使用 JSON 格式傳遞筆記 + JQuery 測試 0 2 因為一些因素,必須改寫WebService,很傳統,但是很多公司還在用.. 因為XML 的關係,不想讓他 ...

  9. 在JSP页面中输出JSON格式数据

    JSON-taglib是一套使在JSP页面中输出JSON格式数据的标签库. JSON-taglib主页: http://json-taglib.sourceforge.net/index.html J ...

随机推荐

  1. .NET 事件

    事件概述                                                                 在发生其他类或对象关注的事情时,类或对象可通过事件通知它们.发 ...

  2. .NET LINQ基本查询操作

    获取数据源      在 LINQ 查询中,第一步是指定数据源.像在大多数编程语言中一样,在 C# 中,必须先声明变量,才能使用它.在 LINQ 查询中,最先使用 from 子句的目的是引入数据源 ( ...

  3. .NET LINQ 生成操作

    生成操作      生成是指创建新的值序列. 方法 方法名 说明 C# 查询表达式语法 Visual Basic 查询表达式语法 更多信息 DefaultIfEmpty 将空集合替换为具有默认值的单一 ...

  4. Linux-iptables初识

    Linux-iptables初识 了解 iptables是与Linux内核集成的IP信息包过滤系统.如果Linux系统连接到因特网或LAN.服务器或连接LAN和因特网的代理服务器,则该系统有利于在Li ...

  5. C#反射机制 Type类型

    using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System ...

  6. PHP获取当前位置

    如果想动态的获取当前栏目所处的位置,就要关联到数据库,下面是例子: 栏目表(category): 涉及到二级目录. (1)首先在你的首页导航栏,还有首页所出现的链接后面,为栏目加上catid,就像这样 ...

  7. GitHub使用心得

    PHP今天算是学习结束了;版本控制也了解很久了,比较熟悉的是svn,Git一直都不太熟,也可能是它是英文的缘故,感觉很费劲,所以用的不多,但不得不说,它功能真的很强大;今天试着和朋友用Git开发一个简 ...

  8. SQLite -- 分页查询

    原文:http://blog.csdn.net/lu1024188315/article/details/51734514 参考:http://www.runoob.com/sqlite/sqlite ...

  9. Spring快速入门

    什么是Spring Spring是分层的JavaSE/EE full-stack(一站式) 轻量级开源框架 分层 SUN提供的EE的三层结构:web层.业务层.数据访问层(持久层/集成层) Strut ...

  10. [原创]Centos7 内部常用软件升级计划

    GCC 当前系统版本 gcc version 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)