【www.bbyears.com--WebService】
jquery代码
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)
{
string method = Request.QueryString["method"];
if("Hello".Equals(method))
{
//把WebService的方法copy过来或者直接调用WebService的方法
}
}