jquery $.ajax json資料傳遞方法

來源:互聯網
上載者:User
protected void page_load(object sender, eventargs e)
{
hashtable ht = new hashtable();
string name = request.params["name"].tostring();
string birth = request.params["birthday"].tostring();
if (!string.isnullorempty(name) && !string.isnullorempty(birth))
{
//response.contenttype = "application/json";
//response.write(crearejson("this is ok!", 1, name, birth));
ht.add("info", "成功了");
ht.add("sta", "狀態");
ht.add("name", name);
ht.add("birth", birth);
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) + "}";
}

前台代碼

 

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.111cn.net/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml" >
<head>
<title>無標題頁</title>
<style type="text/css教程">
.show{ display:block;}
.hide{ display:none;}
</style>
<script type="text/網頁特效" src="jquery/jquery-1.2.6.js"></script>
<script type="text/網頁特效">

//這個方法把ajax方法封裝一下,方便調用。
function myajax(){
//var obj=jsondata();
$.ajax({
type:'post',
url:'ajax.asp教程x',
data:jsondata(),//可以直接加一個函數名。
datatype:'json',
beforesend:beforecall,
success:callback
});
}
//封裝json資料,為了代碼清晰
function jsondata(){
var jsonstr="({";
jsonstr+=""name":";
jsonstr+=""tree"";
jsonstr+=",";
jsonstr+=""id":";
jsonstr+=""123"";
jsonstr+="})";
return eval(jsonstr);//關鍵在於轉換。
}
//調用前方法,不成功
function beforecall(){
$('#wait').addclass("show").append('調出中...');
//alert('');//測試是否調用
}
//回呼函數
function callback(data){
$('#response').append(data.name+data.id);
$('#wait').css("display","none");
}
//onload()事件
$(function(){
$('#confirm').click(myajax);
})
</script>
</head>
<body>
<div id="confirm">點擊</div>
<div id="response">接收後台資料</div>
<div id="wait" class="hide">hello</div>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.