Jquery getJSON()

getJSON與aspx

準備工作

·Customer類

?

public?class?Customer
{
????public?int?Unid?{?get;?set;?}
????public?string?CustomerName?{?get;?set;?}
????public?string?Memo?{?get;?set;?}
????public?string?Other?{?get;?set;?}
}

?

?

(一)ashx

?

Customer?customer?=?new?Customer?
???????{?Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"};

string?strJson?=?Newtonsoft.Json.JsonConvert.SerializeObject(customer);

context.Response.Write(strJson);

?

?

function?GetCustomer_Ashx()?{
????$.getJSON(
????"webdata/Json_1.ashx",
????function(data)?{
????????var?tt?=?"";
????????$.each(data,?function(k,?v)?{
????????????tt?+=?k?+?":"?+?v?+?"<br/>";
????????})

????????$("#divmessage").html(tt);
????});
}

?

·通過getJSON向ashx請求數據。返回的數據為JSON對象。

(二)ashx文件,但返回的是實體集合

?

Customer?customer?=?new?Customer?
????{?Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"};

Customer?customer2?=?new?Customer?
????{?Unid?=?2,?CustomerName?=?"吳用",?Memo?=?"天機星",?Other?=?"智多星"?};????????

List<Customer>?_list?=?new?List<Customer>();
_list.Add(customer);
_list.Add(customer2);????????

string?strJson?=?Newtonsoft.Json.JsonConvert.SerializeObject(_list);
context.Response.Write(strJson);

?

?

?

?

function?GetCustomerList()?{
????$.getJSON(
????"webdata/Json_1.ashx",
????function(data)?{
????????var?tt?=?"";
????????$.each(data,?function(k,?v)?{
????????????$.each(v,function(kk,?vv)?{
????????????????tt?+=?kk?+?":"?+?vv?+?"<br/>";
????????????});
????????});
????????$("#divmessage").html(tt);
????});
}

?

具體可以參看:http://www.cnblogs.com/jams742003/archive/2009/12/25/1632276.html

(三)請求aspx文件

·cs文件

?

protected?void?Page_Load(object?sender,?EventArgs?e)
{
???Customer?customer?=?new?Customer?
?????{?Unid?=?1,?CustomerName?=?"宋江",?Memo?=?"天魁星",?Other?=?"黑三郎"?};

??string?strJson?=?Newtonsoft.Json.JsonConvert.SerializeObject(customer);

??Response.Write(strJson);
}

?

?

?

·Aspx文件

<%@?Page?Language="C#"?AutoEventWireup="true"?CodeFile="Json_1.aspx.cs"?
??Inherits="webdata_Json_1"?%>

?

前臺文件只保留Page聲明,其它全部刪除。

?

·js文件

?

function?GetCustomer_Aspx()?{
????$.getJSON(
????"webdata/Json_1.aspx",
????function(data)?{
???????????????var?tt?=?"";
???????????????$.each(data,?function(k,?v)?{
????????????????????tt?+=?k?+?":"?+?v?+?"<br/>";
????????????????})
????????????????$("#divmessage").html(tt);
????});
}

?

這個部分與請求ashx文件時相同。

請求實體集合時,與ashx時相同,這里不做重復。

(四)請求文本文件

文本文件提供json字符串,由$.getJSON得到json對象。

·文本文件

{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"}

文本文件提供json串,對于json的組成格式,請參見其它文檔。對于這一實體json,會被忽略空行與空格。?

?

function?GetCustomer_txt()?{
????$.getJSON(
????"webdata/Json_1.txt",
????function(data)?{
????????var?tt?=?"";
????????$.each(data,?function(k,?v)?{
????????????tt?+=?k?+?":"?+?v?+?"<br/>";
????????})
????????$("#divmessage").html(tt);
????});
}

?

解析的方法與其它的相同。

?

對于多行的如下:

文本:

[

{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"},

{Unid:2,CustomerName:"吳用",Memo:"天機星",Other:"智多星"}

]

?

解析:

?

function?GetCustomer_TxtList()?{
????$.getJSON(
????"webdata/Json_1.txt",
????function(data)?{
????????var?tt?=?"";
????????$.each(data,?function(k,?v)?{
????????????$.each(v,?function(kk,?vv)?{
????????????????tt?+=?kk?+?":"?+?vv?+?"<br/>";
????????????});
????????});
????????$("#divmessage").html(tt);
????});
}

?

與其它的相同。

(五)帶參數ajax請求

以ashx為例子,按客戶id來請求客戶。

·Ashx文件

?

if(context.Request["iUnid"]==null)
???return;

context.Response.ContentType?=?"text/plain";?

Customer?customer?=?new?Customer?
{?Unid?=?1,?CustomerName?=?"宋江",?Memo?=?"天魁星",?Other?=?"黑三郎"?};

Customer?customer2?=?new?Customer?
{?Unid?=?2,?CustomerName?=?"吳用",?Memo?=?"天機星",?Other?=?"智多星"?};?

List<Customer>?_list?=?new?List<Customer>();
_list.Add(customer);
_list.Add(customer2);
??????

int?iCustomerId?=Convert.ToInt32(context.Request["iUnid"]);
var?cus?=?from?q?in?_list
??where?q.Unid?==?iCustomerId
??select?q;

string?strJson?=?Newtonsoft.Json.JsonConvert.SerializeObject(cus);
context.Response.Write(strJson);

?

·ajax請求?

?

function?GetCustomer_AshxWithPara()?{
????$.getJSON(
????"webdata/Json_2.ashx",
????{?iUnid:?1?},
????function(data)?{
????????var?tt?=?"";
????????$.each(data,?function(k,?v)?{
????????????$.each(v,?function(kk,?vv)?{
????????????????tt?+=?kk?+?":"?+?vv?+?"<br/>";
????????????});
????????});

????????$("#divmessage").html(tt);
????});
}

?

其中參數也是以k/v對格式發出。請求返回的可以看到:在服務端以Customer列表集合返回。

?

?在jquery庫中,getJSON其實是調用的:Query.get(url, data, callback, "json")

這點很重要。

轉載于:https://www.cnblogs.com/yang_mysky/archive/2010/07/27/1785923.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379115.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379115.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379115.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

北京中信銀行總行地址_中信銀行拉薩分行舉行“存款保險標識”啟用和存款保險條例宣傳活動...

11月NOV中信銀行拉薩分行舉行“存款保險標識”啟用和《存款保險條例》宣傳活動揭牌啟用儀式111月Jul根據人民銀行和總行關于“存款保險標識”啟用工作相關要求&#xff0c;分行行領導高度重視“存款保險標識”啟用和《存款保險條例》宣傳活動工作&#xff0c;按照統一工作部署、…

Java ClassLoader getPackage()方法與示例

ClassLoader類的getPackage()方法 (ClassLoader Class getPackage() method) getPackage() method is available in java.lang package. getPackage()方法在java.lang包中可用。 getPackage() method is used to return the package that has been defined in ClassLoader or t…

C---編寫程序:求出1~1000之間能被7或12整除,但不能同時被二者整除的所有整數,將結果保存在數組中,要求程序數據的輸入、計算和輸出均使用函數實現。

編寫程序&#xff1a;求出1~1000之間能被7或12整除&#xff0c;但不能同時被二者整除的所有整數&#xff0c;將結果保存在數組中&#xff0c;要求程序數據的輸入、計算和輸出均使用函數實現。 編程思路&#xff1a;分別編寫函數input()、cal()、output()實現數據的輸入、計算和…

報告稱我國成最大移民輸出國 將形成投資產業鏈(關注)

時代特有的現象&#xff0c;我們應該予以關注 “現在國內房價這么高&#xff0c;政策也看不清&#xff0c;還不如逢高賣掉之前買的幾套房子&#xff0c;一兩套房子的錢辦個投資移民&#xff0c;趁還年輕&#xff0c;拿到綠卡后享受一下美國本國待遇的高等教育了。”廣州&#x…

轉整型_156.Ruby烘焙大理石豆沙吐司解鎖大理石花紋整型

好看又好吃的大理石豆沙面包。紅豆餡均勻分布在松軟細膩的面包體里&#xff0c;手撕著吃&#xff0c;一層層的甜美與溫柔&#xff5e;關于吐司面包&#xff0c;我公眾號里寫過白吐司(基礎款牛奶吐司&#xff0c;超綿鮮奶油吐司)和全麥吐司(基礎款50%全麥吐司&#xff0c;經典燕…

ffmpeg 解碼視頻(h264、mpeg2)輸出yuv420p文件

ffmpeg 解碼視頻&#xff08;h264、mpeg2&#xff09;輸出yuv420p文件 播放yuv可以參考&#xff1a;ffplay -pixel_format yuv420p -video_size 768x320 -framerate 25 out.yuv main.c #include <stdio.h> #include <stdlib.h> #include <string.h>#includ…

VS2010 快捷鍵 (空格顯示 綠點, Tab 顯示箭頭)

VS2010 有用的快捷鍵 &#xff1a; Ctrl r, ctrl w, 切換空格示。 轉載于:https://www.cnblogs.com/fengye87626/archive/2012/11/21/2780716.html

C---編寫程序:實現一個隨堂測試,能進行加減乘除運算。要求如下:(1)隨機產生兩個1~10的正整數,在屏幕上輸出題目,如:5+3=?(2)學生輸入答案,程序檢查學生輸入答案是否正確,若正確,

編寫程序&#xff1a;實現一個隨堂測試&#xff0c;能進行加減乘除運算。要求如下&#xff1a; 1&#xff09;隨機產生兩個1~10的正整數&#xff0c;在屏幕上輸出題目&#xff0c;如&#xff1a;53&#xff1f; 2&#xff09;學生輸入答案&#xff0c;程序檢查學生輸入答案是…

分析一下mp4格式的trak -> mdia -> minf -> stbl -> stts、stsc 這兩個box信息

分析一下mp4格式的trak -> mdia -> minf -> stbl -> stts、stsc 這兩個box信息 &#xff08;因為這兩個box在音頻trak和視頻trak 下都有的&#xff0c;而且都有一個數組的值是比較繞的&#xff09; 目錄&#xff1a;stts&#xff1a;記錄時間戳的&#xff0c;每個s…

利用SQL注入2分鐘入侵網站

說起流光、溯雪、亂刀&#xff0c;可以說是大名鼎鼎無人不知無人不曉&#xff0c;這些都是小榕哥的作品。每次一提起小榕哥來&#xff0c;我的崇拜景仰就如滔滔江水&#xff0c;連綿不絕~~~~&#xff08;又來了&#xff01;&#xff09; 讓我們崇拜的小榕哥最新又發布了SQL注入…

pip安裝deb_技術|如何在 Ubuntu 上安裝 pip

pip 是一個命令行工具&#xff0c;允許你安裝 Python 編寫的軟件包。 學習如何在 Ubuntu 上安裝 pip 以及如何使用它來安裝 Python 應用程序。有許多方法可以在 Ubuntu 上安裝軟件。 你可以從軟件中心安裝應用程序&#xff0c;也可以從下載的 DEB 文件、PPA(LCTT 譯注&#xff…

assubclass_Java類class asSubclass()方法及示例

assubclass類類asSubclass()方法 (Class class asSubclass() method) asSubclass() method is available in java.lang package. asSubclass()方法在java.lang包中可用。 asSubclass() method casts this Class object to denote a subclass of the class denoted by the given…

VB6.0 怎樣啟用控件comdlg32.ocx

VB6.0 怎樣啟用控件comdlg32.ocx 怎樣啟用控件comdlg32.ocx 2008-10-08 09:32 提問者&#xff1a; nefu_20061617 |瀏覽次數&#xff1a;1502次vbs文件中有代碼Set ComDlg CreateObject("MSComdlg.CommonDialog")運行時發生錯誤ActiveX 部件不能創建對象: MSComdlg.…

Python---爬蟲案例

例1、爬取公眾號文章中的圖片。 1&#xff0c;首先打開要獲取公眾號文章的地址 2&#xff0c;按下F12&#xff0c;再按Ctrl Shift C&#xff0c;然后鼠標移動到圖片位置&#xff0c;然后觀察控制臺中顯示圖片對應的代碼位置 3&#xff0c;分析該位置的代碼段 代碼段如下&…

WinCE驅動開發問題精華集錦(二)

轉自&#xff1a;http://hgh123.blog.163.com/blog/static/5980422120086183115543/ 感謝 我怎么能在PB左邊的定制平臺加進我的驅動呢&#xff1f; 兩種辦法&#xff1a; 1、在platform.bib或者project.bib的MODULES部分添加一條語句&#xff0c;例如&#xff1a; MyDriver.dll…

報錯Unable to resolve target android-5

報錯信息&#xff1a;Error:Unable to resolve target android-X&#xff08;X是一個數字&#xff09; 錯誤分析&#xff1a;這種錯誤一般大部分是SDK 版本不符所造成的&#xff0c;一般會在Ecplise工作空間導入項目時候出現此錯誤&#xff0c;一般提示&#xff1a;Error:Unabl…

matlab盒子分形維數_分形維數--matlab

一維曲線分形維數的matlab程序function DFractalDim(y,cellmax)%求輸入一維信號的計盒分形維數%y是一維信號%cellmax:方格子的最大邊長,可以取2的偶數次冪次(1,2,4,8...),取大于數據長度的偶數%D是y的計盒維數(一般情況下D>1),Dlim(log(N(e))/log(k/e)),if cellmaxerror(cel…

Java布爾類toString()方法及示例

Syntax: 句法&#xff1a; public String toString();public static String toString(boolean value);布爾類toString()方法 (Boolean class toString() method) toString() method is available in java.lang package. toString()方法在java.lang包中可用。 toString() metho…

pcm數據編碼成為aac格式文件(可以在酷狗播放)

pcm數據編碼成為aac格式文件&#xff08;可以在酷狗播放&#xff09; 關于其中的aac adts格式可以參考&#xff1a;AAC ADTS格式分析 main.c #include <stdio.h> #include <stdlib.h> #include <stdlib.h>#include <libavcodec/avcodec.h> #include …

Python---實驗九

1、使用標準庫urllib爬取“http://news.pdsu.edu.cn/info/1005/31269.htm”平頂山學院新聞網上的圖片&#xff0c;要求:保存到F盤pic目錄中&#xff0c;文件名稱命名規則為“本人姓名” “_圖片編號”&#xff0c;如姓名為張三的第一張圖片命名為“張三_1.jpg”。 from re imp…