jQuery post數據至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數據至ashx
今天給大家分享一個小功能,在jQuery環(huán)境中,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。
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過來的數據:
下面我們嘗試在實際環(huán)境中,Post一個數據如 “1628”,在FireFox的firebug看到傳送結果如下:

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
jQuery中animate()的使用方法及解決$(”body“).animate({“scrollTop”:top})
這篇文章主要介紹了關于jQuery中animate()的使用方法及解決$("body").animate({"scrollTop":top})不被Firefox支持的問題,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04
jquery.lazyload 實現(xiàn)圖片延遲加載jquery插件
看到了淘寶產品介紹中,圖片是在下拉滾動條時加載,這是一個很不錯的用戶體驗。減少了頁面加載的時間了,也減輕了服務器的壓力,就查了下用JQuery..2010-02-02
使用JQuery快速實現(xiàn)Tab的AJAX動態(tài)載入(實例講解)
這篇文章主要介紹了使用JQuery快速實現(xiàn)Tab的AJAX動態(tài)載入(實例講解)需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12
jQuery+CSS3實現(xiàn)仿花瓣網固定頂部位置帶懸浮效果的導航菜單
這篇文章主要介紹了jQuery+CSS3實現(xiàn)仿花瓣網固定頂部位置帶懸浮效果的導航菜單,可實現(xiàn)頁面向下滑動后導航欄橫向懸浮并固定在頂部的功能,涉及jQuery事件響應及頁面元素屬性動態(tài)修改相關操作技巧,需要的朋友可以參考下2016-09-09







