jquery Ajax调用访问WebService
- 来源:
- 网络
- 收录时间:
- 2017-04-12 05:59:00
- 社会 技术开发 阅读 167333
摘要:本文章来给各位朋友详细介绍关于jquery Ajax调用访问WebService的方法,有需要的朋友可参考对敌。jquery代码 代码如下复制代码 Jquery ajax({ type:post, datatype:json, success:fu
jquery代码
代码如下 | 复制代码 |
Jquery.ajax({ type:'post', datatype:'json', success:function(data){ //do something... }, error:function(error){ //do something... } }); |
[WebMethod]
代码如下 | 复制代码 |
public string Hello(){ //return json //方法1:手工拼接 return "{"Test":"Test-Value","测试":"测试-值"}"; //方法2:序列化json //引入System.Runtime.Serialization.Json.DataContractJsonSerializer //用WriteObject方法序列化成json // 或者用Newtonsoft.Json.dll的JsonConvert.SerializeObject去处理 //方法3:把直接拼接的json串吐到浏览器,不需要return System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.ContentType = "application/json"; System.Web.HttpContext.Current.Response.Expires = 0; System.Web.HttpContext.Current.Response.Cache.SetNoStore(); System.Web.HttpContext.Current.Response.Write(jsonContent); System.Web.HttpContext.Current.Response.End(); } |
这样就实现了,但有的朋友说返回的的xml数据,后来才发现url问题,
代码如下 | 复制代码 |
Page_Load(object sender, EventArgs e) |