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

首頁 > 編程 > JavaScript > 正文

jQuery post數據至ashx實例詳解

2019-11-19 18:55:36
字體:
來源:轉載
供稿:網友
public void ProcessRequest(HttpContext context)    {      context.Response.ContentType = "application/json";      context.Response.ContentEncoding = Encoding.UTF8;      Stream inputStream = context.Request.InputStream;      Encoding encoding = context.Request.ContentEncoding;      StreamReader streamReader = new StreamReader(inputStream, encoding);      string strJson = streamReader.ReadToEnd();      Param p = JsonConvert.DeserializeObject<Param>(strJson);      int top = Convert.ToInt32(p.Top);      string term = p.Term;            //var result = ...      //context.Response.Write(result.ToJson());    }Source Code

jQuery post數據至ashx

今天給大家分享一個小功能,在jQuery環境中,Post data to ashx進行數據交互。

參考下面代碼示例:

$.ajax({           url: '<%= ResolveUrl("~/Handlers/xxx.ashx") %>',              dataType: "json",              type: "POST",              contentType: "application/json; charset=utf-8",              data: JSON.stringify({                top: 10,                term: request.term              }),              success: function (data) {                //...              }            });

jQuery的Post值是使用type: "POST",上傳的數據類型為contentType: "application/json; charset=utf-8"。

從代碼示例中,它有2個參考需要上傳,top,term。

為了更好在ashx能接收,我們寫一個model:

using System;using System.Collections.Generic;using System.Linq;using System.Web;/// <summary>/// Summary description for Param/// </summary>namespace Insus.NET{  public class Param  {    private int _top;    public int Top    {      get { return _top; }      set { _top = value; }    }    private string _term;    public string Term    {      get { return _term; }      set { _term = value; }    }  }}

在ashx處理程序中,我們可以同下面這樣接收Post過來的數據:



下面我們嘗試在實際環境中,Post一個數據如 “1628”,在FireFox的firebug看到傳送結果如下:

在ashx中,接收到的結果如下:

 再進一步得到top的值:

最后得到的是term的值:

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定州市| 佳木斯市| 晋中市| 新和县| 兖州市| 灵丘县| 将乐县| 博乐市| 蒲城县| 咸丰县| 兰州市| 城口县| 乐清市| 中山市| 临湘市| 师宗县| 盐津县| 铁力市| 河曲县| 大名县| 阿坝县| 巨野县| 和静县| 屯昌县| 大连市| 黔西| 海林市| 沙坪坝区| 临朐县| 平阳县| 海盐县| 平阳县| 黄骅市| 饶河县| 徐州市| 剑川县| 横峰县| 潼南县| 临漳县| 吉林市| 定安县|