欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery post數(shù)據(jù)至ashx實例詳解

 更新時間:2016年11月18日 14:37:41   作者:楊明波(Leo Yang)  
這篇文章主要介紹了jQuery post數(shù)據(jù)至ashx實例詳解的相關資料,需要的朋友可以參考下
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數(shù)據(jù)至ashx

今天給大家分享一個小功能,在jQuery環(huán)境中,Post data to ashx進行數(shù)據(jù)交互。

參考下面代碼示例:

$.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",上傳的數(shù)據(jù)類型為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過來的數(shù)據(jù):



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

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

 再進一步得到top的值:

最后得到的是term的值:

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

相關文章

最新評論