passare jQuery Json in http: http: asp.net

Non capisco cosa sto sbagliando .. ho cercato dozzine di domande simili, eppure ho ancora incomprensioni … quando chiamo la funzione CallHandler di JS, ottengo sempre l’avviso di ‘Richiesta non riuscita’. mi aiuti per favore.

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

codice asp.net c # dietro:

 public void ProcessRequest(HttpContext context) { JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); string jsonString = HttpContext.Current.Request.Form["json"]; List emplList = new List(); emplList = jsonSerializer.Deserialize<List>(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; } } 

Provare

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

modifica Ho rimosso le virgolette dai nomi delle proprietà

Inoltre, la stringa JSON deve essere letta in altro modo

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

Una soluzione funzionante

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