2012-09-25 57 views
1

的JSON数据,我将json数据发送到data.ashx文件,但我无法从ProcessRequest读取ashx文件的数据。只是不明白为什么我得到空如何从我的客户端读取ashx文件

这种方式我是从客户端发送数据到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

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); 
    } 

我的ashx文件的代码我也看到数据如何使用firebig进入服务器端。这里是数据 { “名称”: “CVV”, “主题”: “fdsfd”, “电子邮件”: “[email protected]”, “详细信息”: “哇”}

,请帮助我如何从客户端发送json时从ashx文件读取数据。请告诉我我犯了什么错误。请指导我。感谢

+0

在这里,我问过建设成JSON如何从ProcessRequest()方法读取json数据。不是客户端。 – Thomas

+1

http://stackoverflow.com/questions/423294/best-way-to-parse-json-data-into-a-net-object,http://stackoverflow.com/questions/401756/parsing-json-using -json-net,http://stackoverflow.com/questions/1212344/parse-json-in-c-sharp – Andreas

+0

感谢提示,但我正在寻找解决方案,从ashx文件阅读json数据意味着asp.net httphandler。 – Thomas

回答

4

第一点要注意的是,确保你总是在context.Request对象检查空或空字符串

接下来是你的反应应该是一个JSON对象,但你只是返回一个字符串.. 从ashx的处理程序发送

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); 
    } 

});​ 
相关问题