error de publicación jquery 500

I have used the JavaScript code to call a method in the c# class (method provided in code). When is does a post back I get an error 500, I would like to know how I can fix this so that I call the method.

JavaScript to call c# class method

$.ajax({
type: "post",
    url: "TestChapter.aspx/timeFinished",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        //
    }
});

Method in C# class testchapter.aspx:

[System.Web.Services.WebMethod]
public void timeFinished() {
    // my code is here 
}

preguntado el 28 de mayo de 14 a las 12:05

Debug your method, it's throwing an error -

Que pasa cuando llamas TestChapter.aspx/timeFinished en el navegador? -

Statuscode 500 means internal server error. So the code in your timeFinished method is throwing an unhandled Exception. To help you, we'll need your code for that method and some more information on the error -

Since you're requesting JSON format, your method should be decorated with [ScriptMethod] (unless your web service class is already decorated with [ScriptService]). -

2 Respuestas

Try this method in C# class testchapter.aspx, it might work:

[System.Web.Services.WebMethod]
public static void timeFinished() {
    // my code is here 
}  

Echa un vistazo a esta publicación

contestado el 28 de mayo de 14 a las 12:05

One thing is that you certainly missed the / char in your ajax method url, it should be:

url: "/TestChapter.aspx/timeFinished",

The other thing is that you should log the error to the console, it's easier that way:

$.ajax({
     type: "post",
     url: "/TestChapter.aspx/timeFinished",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function (result) {
         // do something
     },           
     error: function (data) {  // handle your error loging in here
         var errorDetails = data.responseText.replace("{", "").split(',');
         var errorMessage = "";
         for (var i = 0; i < errorDetails.length; i++) {
            if (errorDetails[i].split(':')[0] == "\"Message\"") {
                errorMessage = errorDetails[i].split(':')[1];
            }
         }
            alert("Error:" + errorMessage);
});

It will write all errors in the debugger console, it's a lot easier to find errors.

contestado el 28 de mayo de 14 a las 12:05

@WilliamBarbosa Yes, it does. No / in the url is an error, so adding it is needed. Logging the internal server error is also a solution, because ot shows what is wrong on server side. - kamil t

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.