一般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. OUC校园导游

    大二专业课太多,都没有好好的在博客上面做笔记,以备后面用的时候可以查找看一下,下面是写的不是完全正确的与图相关的代码~~希望指正~~ /* Name: Copyright: Author:Hxm Da ...

  2. 16. 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  3. Qt 配置文件QSettings

    QString appDirPath = QCoreApplication::applicationDirPath(); QSettings* configIniRead = new QSetting ...

  4. socket通信之eofexception

    方案一: 用read()将不会抛出异常.因为read是block方式的. readInt()抛出异常的原因(我遇到的)是对方在该端口没有消息发送. 方案二: socket的端口被阻塞了 ,也就是我们需 ...

  5. js验证输入的金钱格式

    <html> <head> <title>js验证输入的金钱格式</title> <script type="text/javascri ...

  6. JDK各个版本的新特性jdk1.5-jdk8

    JDK各个版本的新特性 对于很多刚接触java语言的初学者来说,要了解一门语言,最好的方式就是要能从基础的版本进行了解,升级的过程,以及升级的新特性,这样才能循序渐进的学好一门语言.今天先为大家介绍一 ...

  7. Mac系统 安装SVN

  8. HDU 1536 S-Nim SG博弈

    S-Nim Problem Description   Arthur and his sister Caroll have been playing a game called Nim for som ...

  9. 爱特梅尔ATMEL全系列芯片解密单片机破解

    爱特梅尔ATMEL全系列芯片解密单片机破解 ATMEL芯片介绍: ATMEL公司为全球性的业界领先企业,致力于设计和制造各类微控制器.电容式触摸解决方案.先进逻辑.混合信号.非易失性存储器和射频 (R ...

  10. 关于 《cocoapods 的taobao的镜像停止更新问题》

    cocoapods 的ruby.taobao.org 停止更新了!!!!!! 最近一直在改老项目,突然听伙伴说:cocoapods 的ruby.taobao.org 停止更新了.很是吃惊,遂即闯入:h ...