一般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. 【python】继承关系和isinstance

    来源:廖雪峰 继承关系是: object -> Animal -> Dog -> Husky 那么,isinstance()就可以告诉我们,一个对象是否是某种类型.先创建3种类型的对 ...

  2. Hotaru's problem(hdu 5371)

    题意:给出一个数字串,询问最长的子串,满足以下要求:将子串平均分为三部分,一三部分相等,一二部分对衬. /* 在manachar的基础上,枚举回文串的中心,再找第三部分. */ #include< ...

  3. LeetCode之136. Single Number

    -------------------------------------- 一个数异或它自己会得到0,0异或n会得到n,所以可以用异或来消除重复项. AC代码如下: public class Sol ...

  4. userdel 连同家目录一起删除

    userdel -r xxx 连同家目录一起删除

  5. Python requests模拟登录

    Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...

  6. 第二十八篇:SOUI中自定义控件开发过程

    在SOUI中已经提供了大部分常用的控件,但是内置控件不可能满足用户的所有要求,因此一个真实的应用少不得还要做一些自定义控件. 学习一个新东西,最简单的办法就是依葫芦画瓢.事实上在SOUI系统中内置控件 ...

  7. Git Push 避免用户名和密码方法

    参考这里: http://www.cnblogs.com/ballwql/p/3462104.html 亲测第一种方法有效

  8. threadid=1: thread exiting with uncaught exception (group=0xb2a86d70)

    这个错误是程序运行成功,但是一在虚拟机上运行就报“停止运行了”,如图: 然后我们查看一个日志信息: 查了好久,终于 搞好了,原因是安卓目标SDK版本太高了,我这里创建好默认是这样的如图: 只要把21改 ...

  9. Linux学习笔记(3)-常用命令

    江湖传言,Linux和Windows不同,他主要的用途是在一些服务器,或者片内系统上,所以人机交互界面自然就没有Windows那么漂亮,其实也没有那个必要. 所以,学习Linux的第一步,就是学习他那 ...

  10. Python笔记-第一天

    1.Python的输出print函数要把输出的字符串用单引号或者双引号括起来,但是不能混用. 比如print('hello,world')和print("hello,world") ...