【jquery ajax】jquery Ajax调用访问WebService

更新时间:2017-12-21    来源:WebService    手机版     字体:

【www.bbyears.com--WebService】


jquery代码

 代码如下 Jquery.ajax({   
  
    type:"post",   
  
    datatype:"json",   
  
    success:function(data){   
  
        //do something...   
  
    },   
  
    error:function(error){   
  
        //do something...   
  
    }   
  
});   
  

WebService处理

[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的方法   
    }   
  
}   
  

本文来源:http://www.bbyears.com/asp/37932.html

热门标签

更多>>

本类排行