【问题标题】:pass jquery json into asp.net httphandler将 jquery json 传递到 asp.net httphandler
【发布时间】:2012-09-06 05:52:07
【问题描述】:

只是不要明白我做错了什么.. 我一直在寻找几十个类似的问题,但仍然有误解...... 当我从 JS 调用 CallHandler 函数时,我总是收到“请求失败”警报。 请帮帮我。

JS/Jquery:

function CallHandler() {
    $.ajax({
        url: "DemoHandler.ashx",
        contentType: "application/json; charset=utf-8",
        type: 'POST',
        dataType: "json",
        data: [{"id": "10000", "name": "bill"},{"id": "10005", "name": "paul"}],
        success: OnComplete,
        error: OnFail
    });
    return false;
}

function OnComplete(result) {
    alert(result);
}
function OnFail(result) {
    alert('Request Failed');
}

asp.net c# 背后的代码:

public void ProcessRequest(HttpContext context)
{
  JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
  string jsonString = HttpContext.Current.Request.Form["json"];

  List<Employee> emplList = new List<Employee>();
  emplList = jsonSerializer.Deserialize<List<Employee>>(jsonString);

  string resp = "";
  foreach (Employee emp in emplList){
  resp += emp.name + " \\ ";
  }
  context.Response.Write(resp);
}

public class Employee
{
  public string id { get; set; }
  public string name { get; set; }
}

【问题讨论】:

    标签: c# jquery asp.net json httphandler


    【解决方案1】:

    试试

    data: JSON.stringify([{id: "10000", name: "bill"},{id: "10005", name: "paul"}])
    

    编辑我删除了属性名称中的引号

    另外,需要读取JSON字符串in other way

    string jsonString = String.Empty;
    
    HttpContext.Current.Request.InputStream.Position = 0;
    using (StreamReader inputStream = new StreamReader(HttpContext.Current.Request.InputStream))
    {
         jsonString = inputStream.ReadToEnd();
    }
    

    一个可行的解决方案

    public void ProcessRequest(HttpContext context)
    {
        var jsonSerializer = new JavaScriptSerializer();
        var jsonString = String.Empty;
    
        context.Request.InputStream.Position = 0;
        using (var inputStream = new StreamReader(context.Request.InputStream))
        {
            jsonString = inputStream.ReadToEnd();
        }
    
        var emplList = jsonSerializer.Deserialize<List<Employee>>(jsonString);
        var resp = String.Empty;
    
        foreach (var emp in emplList)
        {
            resp += emp.name + " \\ ";
        }
    
        context.Response.ContentType = "application/json";
        context.Response.ContentEncoding = Encoding.UTF8;
        context.Response.Write(jsonSerializer.Serialize(resp));
    }
    
    public class Employee
    {
        public string id { get; set; }
        public string name { get; set; }
    }
    
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    

    【讨论】:

    • ty,我更改了您发布的代码,但仍然只收到“请求失败”警报。
    • 欢迎您,祝您编码愉快!
    • 您可以使用 JSON.Net 以获得良好的性能。james.newtonking.com/projects/json-net.aspx
    猜你喜欢
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多