简体   繁体   English

使用ajax调用WebMethod时出现500 Internal Server Error

[英]Getting 500 Internal Server Error when calling a WebMethod using ajax

I am having an issue hitting my C# WebMethod on the code behind and getting 500 internal server error. 我遇到的问题是,在我的C#WebMethod后面的代码上遇到了问题,并收到500内部服务器错误。 I don't really understand why it won't hit it so it would be really great if someone could tell me what is the problem. 我真的不明白为什么它不会成功,所以如果有人能告诉我问题出在哪里,那真的很棒。

在此处输入图片说明

So this is my ajax call and doesn't work even with data and datatype not commented out. 所以这是我的ajax调用,即使数据和数据类型未注释掉也不起作用。

$('#chkBxAutoCost')
    .click(function(e) {
            $.ajax({
                type: "POST",
                url:   "BatchReportCriteriaSelection.aspx/GetAutoJsonBatches",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                data: "{}",
                error: function(XMLHttpRequest, textStatus,    errorThrown) {
                    console.log("Request: " +
                        XMLHttpRequest.toString() +
                        "\n\nStatus: " +
                        textStatus +
                        "\n\nError: " +
                        errorThrown);
                },
                success: function() { console.log("success") }
            });
        }
    );

And this is my code behind method of the page: 这是我的页面方法背后的代码:

[WebMethod]
public string GetAutoJsonBatches()
{
    return autoJsonBatches;
}

So I have attached a breakpoint on this WebMethod and it's not being hit. 因此,我在此WebMethod上附加了一个断点,但没有被击中。 I am pretty stuck so if someone had any insight I'd greatly appreciate it. 我很困,所以如果有人有任何见解,我将不胜感激。

So as botond said in the comments my problem was that webmethods need to be static! 因此,正如波顿在评论中所说,我的问题是网络方法必须是静态的! Thanks very much was really wrecking my head! 非常感谢,真的让我很头疼!

First you have edit RouteConfig.cs and use 首先,您需要编辑RouteConfig.cs并使用

settings.AutoRedirectMode = RedirectMode.Off;

Next edit your GetAutoJsonBatches() to static 接下来将您的GetAutoJsonBatches()编辑为static

They're static because they are entirely stateless, they don't create an instance of your page's class and nothing is passed to them in the request (ie ViewState and form field values). 它们是静态的,因为它们完全是无状态的,它们不创建页面类的实例,并且请求中没有任何内容传递给它们(即ViewState和form字段值)。

HTTP is stateless by default, ASP.Net does a lot of stuff in the background with ViewState, Session, etc. during a standard page request to make life easier for developers. HTTP默认情况下是无状态的,在标准页面请求期间,ASP.Net在后台使用ViewState,Session等在后台做很多工作,从而使开发人员的工作更加轻松。

Source 资源

[WebMethod]
public static string GetAutoJsonBatches()
{
    return autoJsonBatches;
}

To allow this Web Service to be called from script, using ASP.NET AJAX, you need to use [System.Web.Script.Services.ScriptService] attribute for your WebService. 若要使用ASP.NET AJAX从脚本中调用此Web服务,您需要为WebService使用[System.Web.Script.Services.ScriptService]属性。

[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

    [WebMethod]
    public string GetAutoJsonBatches()
    {
        return autoJsonBatches;
    }
}

And you haven't provided us what exactly is the exception message. 您还没有向我们提供异常消息的确切含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM