Estoy tratando de aprender JSON y jQuery y antes de profundizar en algo demasiado complejo, pensé en probar un ejemplo simple para ponerme al día.
Tengo un botón que, cuando se hace clic, me gustaría pasar un objeto JSON a un WebMethod, realizar algunas operaciones de cadena en él y volver al lado del cliente.
Sin embargo, cuando hago clic en el botón, no sucede nada, la consola del navegador muestra el siguiente error:
POST http://localhost:53388/myfolder/foo/aspx/setrecord 500 (Error interno del servidor)
cualquier ayuda será apreciada
jQuery
$('#SaveButton').click(function () { var jsonObj = { "data":[{ "id": 1, "name": "Zippy" }, { "id": 2, "name": "George" }, { "id": 3, "name": "Bungle" }, { "id": 4, "name": "geoffrey"}] } jsonObj = JSON.stringify(jsonObj); SaveNewOrder(jsonObj); }) function SaveNewOrder(jsonObj) { $.ajax({ type: "POST", url: "foo.aspx/setrecord", data: jsonObj, contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, failure: function (response) { alert(response.d); } }); } function OnSuccess(response) { $('#OutputDiv').html(response.d); }Clases y WebMethod
public class ListOfDestinationColumns { public List<DestinationColumn> data { get; set; } } public class DestinationColumn { public int id { get; set; } public string name { get; set; } } [WebMethod] public static string setrecord(string jsonObj) { ListOfDestinationColumns Destinations = new JavaScriptSerializer().Deserialize<ListOfDestinationColumns>(jsonObj); string output = "Start<br/>"; foreach (var item in Destinations.data) { output += string.Format("id: {0}, name: {1}<br />", item.id.ToString(), item.name); } output += "End"; return output; }