pass jquery json into asp.net httphandler

前端 未结 1 1952
我寻月下人不归
我寻月下人不归 2020-12-01 09:49

Just don\'t get it what i\'m doing wrong.. i\'ve been looking for dozens of similar questions, yet still got misunderstandings... when i call CallHandler function from JS, i

相关标签:
1条回答
  • 2020-12-01 10:16

    Try

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

    edit I removed the quotes from the property names

    Also, the JSON string needs to be read 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();
    }
    

    An working solution

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题