用Jquery訪問WebService并返回Json的代碼第1/3頁
更新時間:2008年09月15日 22:37:34 作者:
經(jīng)常會用JavaScript訪問asp.net的Webservice的需求,通常的方法是用asp.net ajax來解決,但asp.net ajax框架在不國內(nèi)并不經(jīng)常被使用。
在我們的應用中一般會是這樣的,使用了jquery作為客戶端框架,ajax請求也通常返回html或者json。html這里就不討論了。返回json一般都是搞一個handler.ashx來處理請求,拼湊字符串來返回json。從而放棄了ws,因為ws返回的是xml,使用起來不方便。
所以我覺著比較完美的解決方法是讓ws返回json而且不用asp.net ajax的客戶端框是比較理想的解決方法。
通過觀測發(fā)現(xiàn)asp.net ajax的客戶端框架請求webservice的時候返回的是json,為什么webservice沒有返回xml而返回了json呢?抓包分析到,關鍵在request的headers中 “Content-Type: application/json;utf-8” ,因此webservice就使用了json的序列化,應該是“System.Web.Script.Serialization.JavaScriptSerializer”這個類完成的工作,通過web.config的配置,把*.asmx交給了System.Web.Extensions.Dll。也就是這里還是用了asp.net ajax,不過是用的服務端部分,我這里直接用的asp.net 3.5
以上都是在啰嗦,具體的方法很簡單,看例子
ws1.asmx
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace test2
{
/// <summary>
/// Summary description for WS1
/// </summary>
[WebService(Namespace = "http://onewww.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WS1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public TestUser CreateUser(string name,int age)
{
return new TestUser { Name = name, Age = age };
}
}
public class TestUser
{
public string Name { get; set; }
public int Age { get; set; }
}
}
所以我覺著比較完美的解決方法是讓ws返回json而且不用asp.net ajax的客戶端框是比較理想的解決方法。
通過觀測發(fā)現(xiàn)asp.net ajax的客戶端框架請求webservice的時候返回的是json,為什么webservice沒有返回xml而返回了json呢?抓包分析到,關鍵在request的headers中 “Content-Type: application/json;utf-8” ,因此webservice就使用了json的序列化,應該是“System.Web.Script.Serialization.JavaScriptSerializer”這個類完成的工作,通過web.config的配置,把*.asmx交給了System.Web.Extensions.Dll。也就是這里還是用了asp.net ajax,不過是用的服務端部分,我這里直接用的asp.net 3.5
以上都是在啰嗦,具體的方法很簡單,看例子
ws1.asmx
復制代碼 代碼如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace test2
{
/// <summary>
/// Summary description for WS1
/// </summary>
[WebService(Namespace = "http://onewww.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WS1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public TestUser CreateUser(string name,int age)
{
return new TestUser { Name = name, Age = age };
}
}
public class TestUser
{
public string Name { get; set; }
public int Age { get; set; }
}
}
您可能感興趣的文章:
- 淺談對Jquery+JSON+WebService的使用小結
- jQuery調(diào)用WebService返回JSON數(shù)據(jù)及參數(shù)設置注意問題
- 排除JQuery通過HttpGet調(diào)用WebService返回Json時“parserror”錯誤
- jQuery結合Json提交數(shù)據(jù)到Webservice,并接收從Webservice返回的Json數(shù)據(jù)
- asp.net下使用jquery 的ajax+WebService+json 實現(xiàn)無刷新取后臺值的實現(xiàn)代碼
- jQuery調(diào)用Webservice傳遞json數(shù)組的方法
相關文章
使用ASP.NET?Web?API構建Restful?API
這篇文章介紹了使用ASP.NET?Web?API構建Restful?API的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04ADO.NET 讀取EXCEL的實現(xiàn)代碼((c#))
經(jīng)常需要在數(shù)據(jù)庫與Execl之間互導數(shù)據(jù)。net時代,ADO.NET可以使用使用Microsoft.Jet.OleDb訪問訪問Excel,網(wǎng)上已經(jīng)有很多類似的資源,最典型也是最簡單的可能如下:(asp.net環(huán)境)2012-12-12