Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

c# - pass jquery json into asp.net httphandler

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 always get 'Request Failed' alert. please help me.

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# code behind:

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; }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...