0

我试图将 JSON 从 jQuery 传递到 .ASHX 文件。我想通过 HttpContext 和猫检索 .ASHX 文件中的 JSON 数据到 NameValueCollection。怎么做?

$.ajax({
    url: "GetLetterInformationHandler.ashx",
    data: "{'Name':'david', 'Family':'logan'}",
    contentType: "application/json; charset=utf-8",
    type: "Get",
    datatype: "json",
    onSuccess: function (data) {

    }
}); 

现在我可以使用查询字符串和 And cast 如下:

  public void ProcessRequest(HttpContext context)
    {
        HttpResponse response = context.Response;
        string cururl = context.Request.Url.ToString();
        int iqs = context.Request.Url.ToString().IndexOf('?');
        string querystring = (iqs < cururl.Length - 1) ? cururl.Substring(iqs + 1) : String.Empty;
        NameValueCollection parameters = HttpUtility.ParseQueryString(querystring);
        context.Response.ContentType = "text/plain";
    }

我想使用查询字符串的 json

4

2 回答 2

0

我做了一些更改,希望对您有所帮助:)

阿贾克斯函数

$.ajax({
  type: "POST",
  url: "GetLetterInformationHandler.ashx",
  data: "{'Name':'david', 'Family':'logan'}",
  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);
         }
  });

.ASHX 文件

public void ProcessRequest(HttpContext context) 
{     
   string outputToReturn = String.Empty;   // Set it to Empty Instead of  ""        
   context.Response.ContentType = "text/json";     

   var getName = String.Empty ;     
   var getFamily = String.Empty ;      // Make sure if the Particular Object is Empty or not
   if (!string.IsNullOrEmpty(context.Request["Name"]))         
     {           
        getName = context.Request["Name"];         
     }

   if (!string.IsNullOrEmpty(context.Request["Subject"]))        
     {             
        getFamily = context.Request["Subject"];         
     }

    NameValueCollection nvc = new NameValueCollection();
    nvc.Add(getName, getFamily);
    var dict = new Dictionary<string, string>();
    foreach (string key in nvc.Keys)
    {
        dict.Add(key, nvc[key]);
    }
    string json = new JavaScriptSerializer().Serialize(dict);
    Console.WriteLine(json);
}
于 2013-05-28T08:13:13.993 回答
0

尝试这个:

$.map(data.d, function (item) {
    return {
       name: item.Name,
       family: item.Family
    };
})

或者,如果您想要每个功能:

var resultData = data.d;
$.each(resultData, function() {
    alert(this)
})
于 2013-05-28T07:40:26.120 回答