传递参数的标准方法(jQuery.ajax)
2008-11-19 08:50
567 查看
前台
<!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) + "}";
}
}
<!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) + "}";
}
}
相关文章推荐
- 传递参数的标准方法(jQuery.ajax)
- 传递参数的标准方法(jQuery.ajax)
- jQuery之异步Ajax请求使用 通过传递参数来调用后台方法返回状态
- JQuery.ajax传递中文参数的解决方法
- 真正的JQuery.ajax传递中文参数的解决方法
- JQuery.ajax传递中文参数的解决方法 推荐
- 这才是真正的JQuery.ajax传递中文参数的解决方法
- 真正的JQuery.ajax传递中文参数的解决方法
- JQuery.ajax传递中文参数的解决方法 推荐
- 这才是真正的JQuery.ajax传递中文参数的解决方法
- 在用ajax调用时在xx.ashx后传递参数,根据传递的参数值的不同,调用xx.ashx中相应的方法
- JQuery中$.ajax()方法参数详解
- ajax回调函数参数传递正确方法
- JQuery中$.ajax()方法参数详解
- JQuery中$.ajax()方法参数详解
- JQuery中$.ajax()方法参数详解(转载)
- JQuery中$.ajax()方法参数详解
- jquery中的ajax方法参数浅谈
- 为jquery的ajaxfileupload增加附加参数的方法
- JQuery中$.ajax()方法参数详解