I'm trying to calling this WebMethod:
[System.Web.Services.WebMethod]
public static List<CustomObject> Carica(int ID)
{
List<CustomObject> elenco = new List<CustomObject>();
try
{
elenco = GetElencoByID(ID);
}
catch (Exception ex)
{
throw ex;
}
return elenco;
}
with Ajax:
function caricaElenco(ID) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "nomePagina.aspx/Carica",
data: '{ID:' + ID + '}',
dataType: "json",
success: function (response) {
for (var i = 0; i < response.d.length; i++) {
alert(response.d[i].nomeCampo);
}
},
error: function (result) {
alert("Errore! " + result.status + " - " + result.statusText);
}
});
}
But I'm always getting error 500, even if debugging the server code, he's doing his job and returning the list.. If I try just for test to modify the function for returning a single object:
public static CustomObject Carica(int ID)
{
(...)
return elenco[0];
}
I'm able to do:
success: function (response) {
alert(response.d.nomeCampo);
}
},
and it's working! I've also tried to change the method signature to:
public static IENumerable<CustomObject> Carica(int ID)
but still no luck.. That's something wrong about ajax and custom object list (the object is [Serializable]?
UPDATE:
Now I've:
[System.Web.Services.WebMethod]
public static void Carica(int ID)
{
List<CustomObject> elenco = new List<CustomObject>();
try
{
elenco = GetElencoByID(ID);
}
catch (Exception ex)
{
throw ex;
}
System.Web.Script.Serialization.JavaScriptSerializer jSearializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
string result = jSearializer.Serialize(flussi);
System.Web.HttpContext.Current.Response.ContentType = "application/json";
System.Web.HttpContext.Current.Response.Write(result);
System.Web.HttpContext.Current.Response.End();
}
But still getting "Errore! 500 - Internal Server Error", but no error debugging server code, I can see in result value [{"ID":1,"campo1":null,"campoData":"/Date(1410166675790)/","campoInt":3858.62},{"ID":2,"campo1":null,"campoData":"/Date(1410166675790)/","campoInt":3858.62} (...)].
If I comment the Response.End() (just to try) I'm getting "Errore! 200 - OK"
UPDATE 2:
OK, I've installed Fiddler, and the error 500 is actually referring to the ThreadAbortException that's coming with the Response.End(). Coming back to the original code with the method returning the List there's the real problem... Exception message: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
UPDATE 3:
OK, I've seen this: Ajax request returns 200 OK, but an error event is fired instead of success and this: jQuery AJAX status "200 OK", but no data response on why I'm getting OK but not success commenting the Response.End().. I don't know if I can put the entire list in a JSON also setting the maxJsonLength to Int32.MaxValue, because is actually a List of 2437 elements with a lot of fields..