DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> 傳遞參數的標准方法(jQuery.ajax)
傳遞參數的標准方法(jQuery.ajax)
編輯:JQuery特效代碼     
前台
代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>無標題頁</title>
<script type="text/javascript" src="jquery/jquery-1.2.6.js"></script>
<script type="text/javascript">
//這個方法把ajax方法封裝一下,方便調用。
function myajax(){
//用此法傳遞數據
var arr={
"name":"tree",
"id":"123"
};
$.get(
"ajax.aspx",
arr,
callback,
"json"
);
}
//回調函數
function callback(data){
$('#response').append(data.name+data.id);
$('#wait').css("display","none");
}
//onload()事件
$(function(){
$('#confirm').click(myajax);
})
//封裝json數據,為了代碼清晰,看來這種方法不行。
/* 似乎是因為數組格式不對。
var dataStr=jsonData();
alert(dataStr);
*/
function jsonData(){
var jsonStr="";
jsonStr+="\"name\":";
jsonStr+="\"tree\"";
jsonStr+=",";
jsonStr+="\"id\":";
jsonStr+="\"123\"";
jsonStr+="}";
return jsonStr;
}
</script>
</head>
<body>
<div id="confirm">點擊</div>
<div id="response">接收後台數據</div>
<div id="wait" class="hide">hello</div>
</body>
</html>

後台
代碼如下:
public partial class ajax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write("hello"+Request["name"]);
Hashtable ht = new Hashtable();
ht.Add("name", Request.Params["name"]);
ht.Add("id", Request.Params["id"]);
Response.Write(CreateJsonParams(ht));
Response.End();
}
private string CreateJsonParams(Hashtable items)
{
string returnStr = "";
foreach (DictionaryEntry item in items)
{
returnStr += "\"" + item.Key.ToString() + "\":\"" + item.Value.ToString() + "\",";
}
return "{" + returnStr.Substring(0, returnStr.Length - 1) + "}";
}
}

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved