本文介绍了通过jQuery的JSON到asp.net的HttpHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是不明白我在做什么错了..
我一直在找几十个类似的问题,但仍然得到了误会......
当我从调用JS CallHandler功能,我总是得到请求失败的提示。
请帮助我。

JS / jQuery的:

 函数CallHandler(){
    $阿贾克斯({
        网址:DemoHandler.ashx
        的contentType:应用/ JSON的;字符集= UTF-8,
        输入:POST,
        数据类型:JSON
        数据:[{ID:10000,名:条例},{ID:10005,名:保罗}],
        成功:的onComplete,
        错误:OnFail
    });
    返回false;
}功能的onComplete(结果){
    警报(结果);
}
功能OnFail(结果){
    警报('请求失败');
}

asp.net C#code背后:

 公共无效的ProcessRequest(HttpContext的背景下)
{
  的JavaScriptSerializer jsonSerializer =新的JavaScriptSerializer();
  字符串jsonString = HttpContext.Current.Request.Form [JSON];  清单<员工> emplList =新的List<员工>();
  emplList = jsonSerializer.Deserialize<名单,LT;员工>>(jsonString);  字符串RESP =;
  的foreach(在emplList雇员EMP){
  RESP + = emp.name +\\\\;
  }
  context.Response.Write(RESP);
}公共类员工
{
  公共字符串ID {搞定;组; }
  公共字符串名称{;组; }
}


解决方案

尝试

 数据:JSON.stringify([{ID:10000,名称:条例},{ID:10005,名称:保罗}])

修改我删除从属性名称引号

此外,JSON字符串需要被读取其他方式 < / p>

 字符串jsonString =的String.Empty;HttpContext.Current.Request.InputStream.Position = 0;
使用(StreamReader的的InputStream =新的StreamReader(HttpContext.Current.Request.InputStream))
{
     jsonString = inputStream.ReadToEnd();
}

这是可行的解决方案。

 公共无效的ProcessRequest(HttpContext的背景下)
{
    VAR jsonSerializer =新的JavaScriptSerializer();
    VAR jsonString =的String.Empty;    context.Request.InputStream.Position = 0;
    使用(VAR的InputStream =新的StreamReader(context.Request.InputStream))
    {
        jsonString = inputStream.ReadToEnd();
    }    VAR emplList = jsonSerializer.Deserialize&LT;名单,LT;员工&GT;&GT;(jsonString);
    VAR RESP =的String.Empty;    的foreach(在emplList VAR EMP)
    {
        RESP + = emp.name +\\\\;
    }    context.Response.ContentType =应用/ JSON;
    context.Response.ContentEncoding = Encoding.UTF8;
    context.Response.Write(jsonSerializer.Serialize(RESP));
}公共类员工
{
    公共字符串ID {搞定;组; }
    公共字符串名称{;组; }
}公共BOOL IsReusable
{
    得到
    {
        返回false;
    }
}

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

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

这篇关于通过jQuery的JSON到asp.net的HttpHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:34