国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 擴(kuò)展 > jQuery > 正文

用Jquery訪問WebService并返回Json的代碼第1/3頁

2024-09-06 20:04:45
字體:
供稿:網(wǎng)友
在我們的應(yīng)用中一般會(huì)是這樣的,使用了jquery作為客戶端框架,ajax請(qǐng)求也通常返回html或者json。html這里就不討論了。返回json一般都是搞一個(gè)handler.ashx來處理請(qǐng)求,拼湊字符串來返回json。從而放棄了ws,因?yàn)閣s返回的是xml,使用起來不方便。
所以我覺著比較完美的解決方法是讓ws返回json而且不用asp.net ajax的客戶端框是比較理想的解決方法。
通過觀測(cè)發(fā)現(xiàn)asp.net ajax的客戶端框架請(qǐng)求webservice的時(shí)候返回的是json,為什么webservice沒有返回xml而返回了json呢?抓包分析到,關(guān)鍵在request的headers中 “Content-Type: application/json;utf-8” ,因此webservice就使用了json的序列化,應(yīng)該是“System.Web.Script.Serialization.JavaScriptSerializer”這個(gè)類完成的工作,通過web.config的配置,把*.asmx交給了System.Web.Extensions.Dll。也就是這里還是用了asp.net ajax,不過是用的服務(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; }
}
}

123下一頁閱讀全文
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 怀来县| 房产| 黄山市| 色达县| 广南县| 清流县| 崇明县| 双江| 会昌县| 恩施市| 红安县| 察哈| 兴宁市| 嘉义市| 广南县| 从江县| 四会市| 永丰县| 旬邑县| 施秉县| 东乡| 集贤县| 普宁市| 宝坻区| 江西省| 顺平县| 长治县| 吉安县| 亳州市| 利川市| 鸡西市| 尼勒克县| 浏阳市| 丽水市| 东平县| 皋兰县| 林西县| 茌平县| 当涂县| 五常市| 扶余县|