ͨ¹ýJquery±éÀúJsonµÄÁ½ÖÖÊý¾Ý½á¹¹
ÔÚajax½»»¥ÖУ¬ÎÒÃÇ´Ó·þÎñÆ÷¶Ë·µ»ØµÄÊý¾ÝÀàÐÍÓÐxml,html,script,json,jsonp,text£¬±¾ÎÄÒÔjsonΪÀý£¬½²ÊöÁËÔÚǰ̨ÈçºÎÀûÓÃjquery±éÀújsonµÄÁ½ÖÖÊý¾Ý½á¹¹£º¡°Ãû³Æ/Öµ¡±¶ÔµÄ¼¯ºÏ£¬ÖµµÄÓÐÐòÁÐ±í£¬ÒÔ¼°ÖµµÄÓÐÐòÁбíÀïÃæ°üº¬¡°Ãû³Æ/Öµ¡±¶ÔµÄ¼¯ºÏ£¬ÔÚ·þÎñÆ÷¶Ë£¬ÎÒÃDzÉÓõÄJson.NETÀ´ÐòÁл¯arraylist,hashTable,list<>µÈÊý¾Ý½á¹¹¡£
ÔÚ¿ªÊ¼Ö®Ç°£¬ÎÒÃÇÐèÒªÏÂÔØJson.net,ÏÂÔØÍê³Éºó£¬ÔÚÍøÕ¾ÖÐÌí¼ÓÒýÓ㬴ò¿ªÏÂÔصÄÎļþ¼Ð£¬Èç¹ûÊÇ.net2.0ÒÔÉϵİ汾£¬Ê¹ÓÃDoNetÎļþ¼ÐϵÄNewtonsoft.Json.dll£¬Èç¹ûÊÇ2.0µÄ°æ±¾£¬Ê¹ÓÃDotNet20ÎļþϵÄNewtonsoft.Json.dll£¬È»ºóÔÚʹÓõÄÒ³Ãæµ¼ÈëÆäÃüÃû¿Õ¼ä using Newtonsoft.Json;
×¼±¸¹¤×÷Íê±Ïºó£¬ÏÂÃ濪ʼÑÝʾ£¬Ê×ÏÈÌí¼ÓwebServiceÎļþ ÃüÃûΪProductService.asmx£¬È»ºóÈ¡Ïû¶Ô[System.Web.Script.Services.ScriptService] µÄ×¢ÊÍ¡£
1¡¢±éÀú ¡°Ãû³Æ/Öµ¡±¶ÔµÄ¼¯ºÏ
ProductService.asmx Ìí¼Ó getProductInfoToJson·½·¨
[WebMethod] public string getProductInfoToJson(int productID) { SQLCMD = new SqlCommand("select id,name,price from dbo.productTest where id=@id", SQLConnect); SQLCMD.CommandType = System.Data.CommandType.Text; SQLCMD.Parameters.AddWithValue("@id", productID); SQLConnect.Open(); SqlDataReader reader = SQLCMD.ExecuteReader(); Hashtable HTresult = new Hashtable(); while (reader.Read()) { HTresult.Add("id", reader["id"]); HTresult.Add("name", reader["name"]); HTresult.Add("price", reader["price"]); } reader.Close(); SQLConnect.Close(); return JsonConvert.SerializeObject(HTresult); }
2¡¢±éÀú ÖµµÄÓÐÐòÁбí[WebMethod]
public string GetProductList(string KeyWord) {
SQLCMD = new SqlCommand("getProductList", SQLConnect);
SQLCMD.CommandType = CommandType.StoredProcedure;
SQLCMD.Parameters.Add(new SqlParameter("@nameKeyWords", SqlDbType.NVarChar, 30));
SQLCMD.Parameters["@nameKeyWords"].Value = KeyWord;
SQLConnect.Open();
SqlDataReader reader = SQLCMD.ExecuteReader();
ArrayList ProductList = new ArrayList();
while (reader.Read())
{
ProductList.Add(reader["name"].ToString());
}
reader.Close();
SQLConnect.Close();
if (ProductList.Count > 0)
{
return JsonConvert.SerializeObject(ProductList);
}
else
{
return "";
}
}
ǰ̨:
var suggestList = $('<ul class="autocomplete"</ul>').hide().insertAfter("#search #search-text");
$("#search-text").keyup(function () {
var textString = "{KeyWord:'" + $("#search #search-text").attr("value") + "'}"
$.ajax({
type: "POST",
url: "ProductService.asmx/GetProductList",
data: textString,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
suggestList.empty();
var objData = jQuery.parseJSON(data.d);
$.each(objData, function (index, term) {
$("<li></li>").text(term).appendTo(suggestList);
});
suggestList.show();
}
});
});
3¡¢±éÀú ÖµµÄÓÐÐòÁбíÀïÃæ°üº¬¡°Ãû³Æ/Öµ¡±¶ÔµÄ¼¯ºÏ
[WebMethod]
public string GetBrandNameByKeyword(string Keyword)
{
SQLCMD = new SqlCommand("BrandInfo_Get_BrandName_UserInputKeyWord", SQLConnect);
SQLCMD.CommandType = CommandType.StoredProcedure;
SQLCMD.Parameters.Add(new SqlParameter("@KeyWord",SqlDbType.NVarChar,10));
SQLCMD.Parameters["@KeyWord"].Value = Keyword;
Hashtable BrandNameInfo;
List<Hashtable> BrandNameInfoCollection = new List<Hashtable>();
SQLConnect.Open();
using (SqlDataReader reader = SQLCMD.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
BrandNameInfo = new Hashtable();
BrandNameInfo.Add("BrandName", reader["BrandName"].ToString());
BrandNameInfo.Add("BrandChineseName", reader["BrandChineseName"].ToString());
BrandNameInfo.Add("nameAbbreviation", reader["nameAbbreviation"].ToString());
BrandNameInfoCollection.Add(BrandNameInfo);
}
SQLConnect.Close();
return JsonConvert.SerializeObject(BrandNameInfoCollection);
}
else
{
SQLConnect.Close();
return null;
}
}
}
ǰ̨
$.ajax({
type: "POST",
url: "ProductService.asmx/GetReceiverAddressInfo",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var resultCollection = jQuery.parseJSON(msg.d);
$.each(resultCollection, function (index, item) {
var AddressInfo = [
'<input type="radio" name="ReceiveAddress" class="address" value="', item.id, '"/> <label class="vtip" title="<font size=3><b>ÊÕ¼þÈË:</b> ', item.ReceiverName, '</br><b>ÁªÏµºÅÂë:</b> ', item.ReceiverPhoneNo, '</br><b>ÏêϸµØÖ·:</b> ', item.DetailsAddress, '</font>">', item.NoticeWords, '</label></br>'
].join('');
});
}
});
ÔÚ1.41ÖУ¬jqueryÌí¼ÓÁË jQuery.parseJSON( json ) µÄ·½·¨£¬¸Ã·½·¨µÄ¶¨ÒåÊÇTakes a well-formed JSON string and returns the resulting JavaScript object. ¾ÍÊǽÓÊÜÒ»¸ö¸ñʽÁ¼ºÃµÄJSON×Ö·û´®£¬·µ»ØÒ»¸öJavascript¶ÔÏó¡£
Õâ´ó´ó·½±ãÁËÎÒÃÇÔÚǰ̨¶Ô·þÎñÆ÷¶ËÉú³ÉµÄJson×Ö·û´®µÄ´¦Àí.
ºÃÁË£¬¹ØÓÚJquery±éÀúJsonÁ½ÖÖÊý¾Ý½á¹¹µÄ½éÉܾ͵½ÕâÀï
ÍƼöÐÅÏ¢
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- jQuery Tooltips²å¼þ
- jQueryµÄ.bind()¡¢.live()ºÍ.delegate(....
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- Ä㲻ϲ»¶»ðºü£¬µ«ÓÖϲ»¶firebug£¬ÄǾ͸úÎÒÒ»ÆðÀ´ÔÚIEÉÏ....
- ¸ù¾ÝÏÔʾÆ÷·Ö±æÂʵ÷ÕûÒ³ÃæÏÔʾµÄJavascript½Å±¾
- jQuery дµÄ·ÂÐÂÀË΢²© ÏòϹö¶¯Ð§¹û¡¾×ªÔØ¡¿
- ͨÓõÄJqueryÑ¡ÔñÆ÷
- jQuery ½áºÏ Json Ìá½»Êý¾Ýµ½Webservice£¬....
- ×ÔÖÆjQueryÖÇÄÜÌáʾ²å¼þһö
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- js»ñÈ¡srcÖд«µÝµÄ²ÎÊý
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- ÈÃä¯ÀÀÆ÷ÆÁ±ÎµôJavaScriptµÄ³ö´íÌáʾ
- github²»ÄܼÓÔØcss¡¢js½â¾ö°ì·¨
- ÎÒÖªµÀµÄJavaScript -- Éè¼Æģʽ(ÇŽÓ)Ó¦ÓÃÖ® ¨C ....
- 15¸öÖµµÃ¿ª·¢ÈËÔ±¹Ø×¢µÄjQuery¿ª·¢¼¼ÇɺÍÐĵÃ
- dz̸JavascriptÃæÏò¶ÔÏó±à³Ì
- JS¼òµ¥¶¯»·â×°
- JavaScript³õѧÕßӦעÒâµÄÆ߸öϸ½Ú
- jQuery Tooltips²å¼þ
ÆÀÂÛ