您的位置:首页 > 编程语言 > ASP

ASP.NET WebForm Ajax请求Handler的经验

2017-08-08 17:04 429 查看

ajax代码

$.ajax({
type: "GET",
url: "/AjaxHandler/GetPluginCode.ashx",
data: "templateid=" + templateid + "&templatepath=<%=templatePath%>&shopgroupid=" + $("#hidShopGroupID").val(),
cache: false,
success: function (msg) {
var results = JSON.parse(msg);
if (results.Key == "success") {
var result = results.Value;
               }
            }
})


    原则:利用对象来判断返回结果的状态(以前用字符串分割来处理,会有问题)

Handler代码

  定义输出对象

public class JsonObj
{
public string Key { get; set; }
public string Value { get; set; }
}


  初始化结果变量

JsonObj _result = new JsonObj() { Key = "failure", Value = string.Empty };


  修改结果状态

try
{
   //逻辑代码
   _result.Key = "success";
  _result.Value = “htmlCode”;
}


  异常捕获

catch (Exception ex)
{
_result.Value = ex.Message;
}


  最后序列化输出

JavaScriptSerializer se = new JavaScriptSerializer();
context.Response.Write(se.Serialize(_result));


  原则:无论如何必须有输出,也就是要有客户端收到结果才能判断请求状态
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐