<script type="text/javascript">
$(function() {
$.ajax({
url: "Handler1.ashx", type: "POST", data: {},
success: function(drow) {
document.write(drow.pics);
docement.write(drow.links);
}
});
});
//上面的ajax方法就是为了从后台返回下面两条语句
// var pics = 'http://jyw.zy528.com/Webs/images/jyw_flash1.jpg|http://jyw.zy528.com/Webs/images/jyw_flash2.jpg|http://jyw.zy528.com/Webs/images/jyw_flash3.jpg|http://jyw.zy528.com/Webs/images/jyw_flash4.jpg';
//var links = 'http://jyw.zy528.com/Webs/pNews/News210.html|http://jyw.zy528.com/giftcard/GetGiftCard.aspx|http://jyw.zy528.com/Webs/pNews/News201.html|http://jyw.zy528.com/activehtm/jyw_activity/index.html';
////////////////////////////////////////////////////////////////////////////
//后面代码省略,太长
</script> 然后下面是后台Handler1中的代码:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
JavaScriptSerializer jss = new JavaScriptSerializer();
string pics = App_Codes.PageProcess.rLinkConfig("pics");
string links = App_Codes.PageProcess.rLinkConfig("links");
Dictionary<string, string> drow = new Dictionary<string, string>();
drow.Add("pics", pics);
drow.Add("links", links);
context.Response.Write(jss.Serialize(drow));
}在最后一句也打了断点,的确有数据返回,如图所示:
然而前台输出的仍然是undefined,到底怎么回事,哪里出错了
这里参数肯定有问题
alert(drow)//显示什么?
}另外可以加
url: "Handler1.ashx", type: "POST", data: {},dataType:"json",后台
context.Response.ContentType = "application/json";
$(function() {
var pics = 'this is pics';
var links = 'this is links';
$.ajax({
url: "Handler1.ashx", type: "POST", data: {},
dataType: "json",
success: function(drow) {
pics = drow.pics;
links = drow.links;
alert(pics);//1此处为后台返回的值
alert(links);//2此处为后台返回的值
}
});
alert(pics);//3this is pic
alert(links);//4this is links
})却发现执行的顺序跟我想的不一样,本以为是1234,结果却是3412。且在函数内部的变量,外面无法访问。请问怎么在函数外访问局部变量,谢谢
url: "Handler1.ashx",
type: "POST",
data: {},
dataType: "json",
async: false,设置 async: false,