用Jquery訪問(wèn)WebService并返回Json的代碼第1/3頁(yè)
更新時(shí)間:2008年09月15日 22:37:34 作者:
經(jīng)常會(huì)用JavaScript訪問(wèn)asp.net的Webservice的需求,通常的方法是用asp.net ajax來(lái)解決,但asp.net ajax框架在不國(guó)內(nèi)并不經(jīng)常被使用。
在我們的應(yīng)用中一般會(huì)是這樣的,使用了jquery作為客戶端框架,ajax請(qǐng)求也通常返回html或者json。html這里就不討論了。返回json一般都是搞一個(gè)handler.ashx來(lái)處理請(qǐng)求,拼湊字符串來(lái)返回json。從而放棄了ws,因?yàn)閣s返回的是xml,使用起來(lái)不方便。
所以我覺(jué)著比較完美的解決方法是讓ws返回json而且不用asp.net ajax的客戶端框是比較理想的解決方法。
通過(guò)觀測(cè)發(fā)現(xiàn)asp.net ajax的客戶端框架請(qǐng)求webservice的時(shí)候返回的是json,為什么webservice沒(méi)有返回xml而返回了json呢?抓包分析到,關(guān)鍵在request的headers中 “Content-Type: application/json;utf-8” ,因此webservice就使用了json的序列化,應(yīng)該是“System.Web.Script.Serialization.JavaScriptSerializer”這個(gè)類完成的工作,通過(guò)web.config的配置,把*.asmx交給了System.Web.Extensions.Dll。也就是這里還是用了asp.net ajax,不過(guò)是用的服務(wù)端部分,我這里直接用的asp.net 3.5
以上都是在啰嗦,具體的方法很簡(jiǎn)單,看例子
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; }
}
}
所以我覺(jué)著比較完美的解決方法是讓ws返回json而且不用asp.net ajax的客戶端框是比較理想的解決方法。
通過(guò)觀測(cè)發(fā)現(xiàn)asp.net ajax的客戶端框架請(qǐng)求webservice的時(shí)候返回的是json,為什么webservice沒(méi)有返回xml而返回了json呢?抓包分析到,關(guān)鍵在request的headers中 “Content-Type: application/json;utf-8” ,因此webservice就使用了json的序列化,應(yīng)該是“System.Web.Script.Serialization.JavaScriptSerializer”這個(gè)類完成的工作,通過(guò)web.config的配置,把*.asmx交給了System.Web.Extensions.Dll。也就是這里還是用了asp.net ajax,不過(guò)是用的服務(wù)端部分,我這里直接用的asp.net 3.5
以上都是在啰嗦,具體的方法很簡(jiǎn)單,看例子
ws1.asmx
復(fù)制代碼 代碼如下:
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; }
}
}
您可能感興趣的文章:
- 淺談對(duì)Jquery+JSON+WebService的使用小結(jié)
- jQuery調(diào)用WebService返回JSON數(shù)據(jù)及參數(shù)設(shè)置注意問(wèn)題
- 排除JQuery通過(guò)HttpGet調(diào)用WebService返回Json時(shí)“parserror”錯(cuò)誤
- jQuery結(jié)合Json提交數(shù)據(jù)到Webservice,并接收從Webservice返回的Json數(shù)據(jù)
- asp.net下使用jquery 的ajax+WebService+json 實(shí)現(xiàn)無(wú)刷新取后臺(tái)值的實(shí)現(xiàn)代碼
- jQuery調(diào)用Webservice傳遞json數(shù)組的方法
相關(guān)文章
詳解c# .net core 下的網(wǎng)絡(luò)請(qǐng)求
本篇文章主要介紹了詳解c# .net core 下的網(wǎng)絡(luò)請(qǐng)求,大致介紹下在.net core 下如何進(jìn)行http請(qǐng)求,主要仍然是GET和POST方法,有興趣的可以了解下2017-05-05使用ASP.NET?Web?API構(gòu)建Restful?API
這篇文章介紹了使用ASP.NET?Web?API構(gòu)建Restful?API的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04asp.net實(shí)現(xiàn)文件無(wú)刷新上傳方法匯總
本文給大家介紹的是asp.net實(shí)現(xiàn)文件無(wú)刷新上傳的2種方法,分別是使用swfupload插件和uploadify插件,講述的十分細(xì)致全面,附上示例,有需要的小伙伴可以參考下。2015-06-06C#讀取中文字符及清空緩沖區(qū)的實(shí)現(xiàn)代碼
有一個(gè)txt的中英文語(yǔ)料庫(kù)文件,內(nèi)容是英文一句中文一句相間的,共3000行,需要把英文句和中文句分開(kāi),放在單獨(dú)的txt文件中。2010-12-12ASP.NET 控件開(kāi)發(fā)系列之圖片切換web控件
剛開(kāi)始學(xué)習(xí)控件開(kāi)發(fā),寫了一個(gè)web圖片切換控件,歡迎大家拍磚.2010-04-04ADO.NET 讀取EXCEL的實(shí)現(xiàn)代碼((c#))
經(jīng)常需要在數(shù)據(jù)庫(kù)與Execl之間互導(dǎo)數(shù)據(jù)。net時(shí)代,ADO.NET可以使用使用Microsoft.Jet.OleDb訪問(wèn)訪問(wèn)Excel,網(wǎng)上已經(jīng)有很多類似的資源,最典型也是最簡(jiǎn)單的可能如下:(asp.net環(huán)境)2012-12-12