简体   繁体   中英

Internal server error when jquery call c# function

I am trying to make c# function call from jquery by using following codes. But I only get Internal server error. Please help me to find out why and how I have to fix it. Thank you.

Script

<script type="text/javascript">
    $(function () {
        $(".matrix_tableActive").click(function (e) {
            alert(e.target.id);
            $.ajax({

                type: "POST",
                url: "Default.aspx/Download",
                data: JSON.stringify({ sth: "hahaha" }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: fnsuccesscallback,
                error: fnerrorcallback
            });
        });




    function fnsuccesscallback(data) {
        alert(data.d);

    }
    function fnerrorcallback(result) {
        alert(result.statusText);
    }

    });
</script>

C#

[WebMethod]
    protected static String Download(String sth)
    {
       return sth;
    }

try calling it like,

window.PageMethods.Download('hahaha',fnsuccesscallback,fnerrorcallback)

This is same as calling AJAX method. except for parameters, OnSuccess and OnError functions, everything else is taken care of internally.

Also according to me web method must be public and static .

Your method should be public as you are calling it from javascript.
It should be like

[WebMethod]
public static String Download(String sth)
{
   return sth;
}

Also to know the error detail you should use the error as below

error: function (xhr, ajaxOptions, thrownError) {
    alert(xhr.status);
    alert(thrownError);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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