jquery发布错误500

时间:2014-05-28 11:50:37

标签: c# javascript

我使用JavaScript代码调用c#类中的方法(代码中提供的方法)。什么时候回帖我得到错误500,我想知道如何解决这个问题,以便我调用该方法。

JavaScript调用c#类方法

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

C#class testchapter.aspx中的方法:

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

2 个答案:

答案 0 :(得分:1)

在C#class testchapter.aspx 中尝试此方法,它可能会有效:

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

查看this post

答案 1 :(得分:0)

有一件事是你肯定错过了你的ajax方法网址中的/字符,它应该是:

url: "/TestChapter.aspx/timeFinished",

另一件事是您应该将错误记录到控制台,这样更容易:

$.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);
});

它将在调试器控制台中写入所有错误,找到错误要容易得多。