一般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');

  

最新文章

  1. teeChart 修改注册表实现无试用到期提示
  2. (转)为什么大公司青睐Java
  3. Solaris 和linux 之oracle 数据库的安装
  4. 微软职位内部推荐-SW Engineer II for Cloud Servi
  5. javascript获取ckeditor编辑器的值(实现代码)
  6. tail报错
  7. 什么是UI控件
  8. springmvc报错 org.springframework.web.servlet.DispatcherServlet
  9. 【protobuf进阶】通过.proto文件导出C#支持的.cs类文件
  10. 命名空间“System.Web.Mvc”中不存在类型或命名空间“Ajax”(是否缺少程序集引用?)
  11. 采用Eclipse中间Maven构建Web项目错误(一)
  12. [译]Java内存泄露介绍
  13. Virgo标签打印
  14. [CSS] input样式定制
  15. 总结Hibernate4.1+版本与Hibernate3.3+版本区别
  16. Dota 2 中安装包的作用
  17. 车轮升级PHP7踩过的一些坑
  18. M1阶段个人总结
  19. 在Windows10中运行debug程序
  20. 201621123001《Java程序设计》第4周学习总结

热门文章

  1. 在VFP6中模拟CursorAdapter的功能
  2. hibernate中HQL多对多的查询
  3. 使用DotNetBar制作漂亮的WinFrom界面,自定义AgileEAS.NET SOA平台WinClient主界面
  4. CSS3动画效果-7.13
  5. php 执行计划任务方式之 linux crontab 执行命令
  6. OSI七层模型及TCP/IP四层模型
  7. Page-encoding specified in XML prolog (UTF-8) is different from that specified in page directive (utf-8)
  8. 怎样上传网页到ftp中
  9. BZOJ4563: [Haoi2016]放棋子
  10. Xcode的清除缓存