如何从ashx文件中读取JSON数据

时间:2012-09-25 14:10:41

标签: jquery asp.net

从我的客户端我发送json数据到data.ashx文件,但我无法从ashx文件的 ProcessRequest 方法读取数据。只是无法理解我为什么会变空

这样我将数据从客户端发送到ashx文件

                var FeedCrd = {};
                FeedCrd["Name"] = $("input[id*='txtName']").val();
                FeedCrd["Subject"] = $("input[id*='txtSubject']").val();
                FeedCrd["Email"] = $("input[id*='txtFEmail']").val();
                FeedCrd["Details"] = $("textarea[id*='txtDetails']").val();


                $.ajax({
                    type: "POST",
                    url: urlToHandler + "?ac=send",
                    data: JSON.stringify(FeedCrd),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        if (data == "SUCCESS");
                        {
            //
                        }

                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }

                });

这是 ProcessRequest

的ashx文件代码
 public void ProcessRequest(HttpContext context)
    {
        string outputToReturn = "";
        context.Response.ContentType = "text/html";

        if (context.Request.QueryString["ac"] == "send")
        {
                string sName = context.Request["Name"];
                string sSubject = context.Request["Subject"];

                outputToReturn = "SUCCESS";
        }
        context.Response.Write(outputToReturn);
    }

我还看到了数据如何使用firebig进入服务器端。这是数据 的 { “名称”: “CVV”, “主题”: “fdsfd”, “电子邮件”: “dsdsa@xx.com”, “详细信息”: “哇”}

所以请帮助我如何从客户端发送json时从ashx文件中读取数据。请告诉我哪里弄错了。请指导我。感谢

1 个答案:

答案 0 :(得分:4)

首先要注意的是确保始终在context.Request对象中检查null或Empty字符串

接下来是你的响应应该是一个JSON对象,但你只是返回一个String .. 在从.ashx处理程序

发送之前构造成JSON
public void ProcessRequest(HttpContext context)
{
    string outputToReturn = String.Empty;   // Set it to Empty Instead of  ""
    context.Response.ContentType = "text/json";
    var ac = string.Empty ;
    var sName = String.Empty ;
    var sSubject = String.Empty ;

    // Make sure if the Particular Object is Empty or not
    // This will avoid errors
    if (!string.IsNullOrEmpty(context.Request["ac"]))
    {
        ac = context.Request["ac"];
    }


    if (ac.Equals("send")) // Use Equals instead of just =  as it also compares objects
    {
        if (!string.IsNullOrEmpty(context.Request["Name"]))
        {
            sName = context.Request["Name"];
        }
        if (!string.IsNullOrEmpty(context.Request["Subject"]))
        {
            sSubject = context.Request["Subject"];
        }
        // You need to Send your object as a JSON Object
        // You are just sending a sting 

        outputToReturn =  String.Format("{ \"msg\" : \"{0}\"  }", "SUCCESS" ) ;
    }
    context.Response.Write(outputToReturn);
}

//在这种情况下你的ajax应该是这样的

$.ajax({
    type: "POST",
    url: urlToHandler + "?ac=send",
    data: JSON.stringify(FeedCrd),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
        if( data != null){
            if (data.msg == "SUCCESS"); {

              alert( data.msg)
            }
        }
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus);
    }

});​