使用jQueryAjax调用C#函数时发生内部服务器错误

本文关键字:内部 服务器 错误 jQueryAjax 调用 函数 使用 | 更新日期: 2023-09-27 18:22:06

在我的应用程序中,我有两个ImageButton。在它们的OnClientClick事件中,我调用js上面的js函数。作为参数,我给出了类似于"en"或"de"的字符串。

Javascript代码:

    //Blocks the submit of the form and changes the language
    function changeLanguage(languageIdentifier)
    {
        var pageUrl = '<%= ResolveUrl("default.aspx/changeLanguage") %>';
        var parameter = '{ "languageIdentifier": languageIdentifier }';
        //Call the C# Methods
        $.ajax({ type: "POST", url: pageUrl, data: parameter, contentType: "application/json; charset=utf-8", dataType: "json", failure: function (response) { alert(response.data) } }).done(function () { alert("Language was changed.") }).fail(function (jqXHR, errorText, errorThrown) { alert("An error has occurred: " + errorThrown); });
        return false;
    }

C#代码:

    [System.Web.Services.WebMethod]
    public static bool changeLanguage(string languageIdentifier)
    {
        try
        {
            //Get the whole formular
            Page thisPage = (Page)HttpContext.Current.Handler;
            //Get the informations from the formular
            Brain.UserRequest = _default.getUserGivenInformation(thisPage);
            //Change session values
            switch (languageIdentifier)
            {
                case "DE":
                case "de-DE":
                case "de": HttpContext.Current.Session["uiculture"] = "de";
                    HttpContext.Current.Session["culture"] = "de-DE";
                    break;
                case "EN":
                case "en-GB":
                case "en":
                default: HttpContext.Current.Session["uiculture"] = "en";
                    HttpContext.Current.Session["culture"] = "en-GB";
                    break;
            }
            //Reloads the page
            HttpContext.Current.Server.Transfer(HttpContext.Current.Request.Url.PathAndQuery, false);
            return true;
        }
        catch 
        {
            return false;
        }
    }

单击ImageButton后,将不会调用C#方法。只显示类似"发生错误:内部服务器错误"的错误消息。有人知道为什么会出现问题吗?

编辑:

现在我已经替换了HttpContext.Current.Server.Transfer代码,并插入了两个响应写入方法。

    [System.Web.Services.WebMethod]
    public static string changeLanguage(string languageIdentifier) 
    {
        HttpContext.Current.Response.Write("Test #default.changeLanguage");
        try
        {
            //Get the whole formular
            Page thisPage = (Page)HttpContext.Current.Handler;
            //Get the informations from the formular
            Brain.UserRequest = _default.getUserGivenInformation(thisPage);
            //Change session values
            switch (languageIdentifier)
            {
                case "DE":
                case "de-DE":
                case "de": HttpContext.Current.Session["uiculture"] = "de";
                    HttpContext.Current.Session["culture"] = "de-DE";
                    break;
                case "EN":
                case "en-GB":
                case "en":
                default: HttpContext.Current.Session["uiculture"] = "en";
                    HttpContext.Current.Session["culture"] = "en-GB";
                    break;
            }
            //Create url for redirecting
            string url = HttpContext.Current.Request.Url.AbsoluteUri;
            return url;
        }
        catch (Exception e)
        {
            HttpContext.Current.Response.Write(e.Message + " #default.changeLanguage");
            return "";
        }
    }

但没有出现来自C#代码的故障诊断码。我发现了来自chrome 的错误消息

Onedrive 上的故障图片

对我来说,找不到C#函数是很困难的。

使用jQueryAjax调用C#函数时发生内部服务器错误


JavaScriptCode

function changeLanguage(languageIdentifier) {
        var pageUrl = '<%= ResolveUrl("/demo.aspx/changeLanguage") %>';
        var params={};
        params.languageIdentifier=languageIdentifier;
        var jsonString = JSON.stringify(params);
        //Call the C# Methods
        $.ajax({
            type: "POST",
            url: pageUrl,
            data: jsonString,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () {
                alert("Language was changed.");
            }
        })
        return false;
    }

.aspx代码

<div>
        <asp:ImageButton ID="imgbutn" runat="server" Height="50" Width="50" OnClientClick="return changeLanguage('en')" />
    </div>

代码隐藏页

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) // to check page is post back or not
        {
            // do stuff here

        }
        if (Request.Headers["X-Requested-With"] == "XMLHttpRequest")  // this is to check whether it's an AJAX call or not
        {
            // do stuff here..
        }
    }
    [System.Web.Services.WebMethod]
    public static bool changeLanguage(string languageIdentifier)
    {
        // do logic here....
        return true;
    }

我刚刚检查了一下它是否正常工作。希望它能帮助你。