webform net method examples example asp c# jquery asp.net asp.net-ajax

c# - net - Mensaje: Primitiva JSON no válida: método jja java ajax con método Web



jquery ajax vb net webmethod (2)

Estoy utilizando el valor de datos como objeto literal, en lugar de concatenar una cadena como se explica en esta respuesta

Mi código es el siguiente:

$.ajax({ url: "../Member/Home.aspx/SaveClient", type: "POST", async: false, dataType: ''json'', contentType: ''application/json; charset=utf-8'', data: { "projectSoid": ProjectId, "startDate": StartDate, "endDate": EndDate, "clientManager": ClientManager }, success: function(response) { if (response.d != "") { } }, error: function(response) { var r = jQuery.parseJSON(response.responseText); alert("Message: " + r.Message); alert("StackTrace: " + r.StackTrace); alert("ExceptionType: " + r.ExceptionType); } })

y mi método web es así:

[WebMethod] public static string SaveClient(string projectSoid, string startDate, string endDate, string clientManager) { ... }

Pero me sale el siguiente error:

Mensaje: primitiva JSON no válida: projectSoid


Con su contentType: ''application/json; charset=utf-8'' contentType: ''application/json; charset=utf-8'' usted reclama que enviará JSON pero actualmente su propiedad de data no contiene JSON.

JSON.stringify transformar sus data a JSON con el método JSON.stringify :

Así que cambia tu propiedad de data a:

data: JSON.stringify({ "projectSoid": ProjectId, "startDate": StartDate, "endDate": EndDate, "clientManager": ClientManager }),

Debe tener en cuenta que el método JSON.stringify no se admite de forma nativa en navegadores antiguos, por lo que es posible que deba proporcionar una implementación utilizando una de las diversas bibliotecas, como:

La json2 Douglas Crockford json2


Javascript en el lado del cliente

var items = [{ projectSoid: ProjectId, startDate: StartDate, endDate: EndDate, clientManager: ClientManager }]; $.ajax({ url: ''"../Member/Home.aspx/SaveClient'', type: "POST", data: JSON.stringify({ items: items }), //data: JSON.stringify("{DocKey : ''" + DocKey + "'',highlightText: ''" + JSON.stringify(text) + "'',pageNo: ''" + pgNo + "'',left: ''" + left + "'',top: ''" + top + "'',width: ''" + width + "'',height: ''" + height + "''}"), //data: "{DocKey/":/""+ DocKey+"/",/"highlightText/":/""+ text +"/",/"pageNo/":/""+pgNo+"/",/"left/":/""+left+"/",/"top/":/""+top+",/"width/":/""+width+"/",/"height/":/""+ height +"}}", // data: "{DocKey : ''" + DocKey + "'',highlightText: ''" + text + "'',pageNo: ''" + pgNo + "'',left: ''" + left + "'',top: ''" + top + "'',width: ''" + width + "'',height: ''" + height + "''}", contentType: "application/json; charset=utf-8", dataType: "json", beforeSend: function () { alert("Start!!! "); }, success: function (data) { alert("Save data Successfully"); }, failure: function (msg) { alert("Sorry!!! "); evt.obj.deleteObject(); }, async: false });

Método web en el código detrás

[WebMethod] public static string SaveClient(object items) { List<object> lstItems = new JavaScriptSerializer().ConvertToType<List<object>>(items); Dictionary<string, object> dic = (Dictionary<string, object>)lstItems[0]; }