服务器端c#方法没有从ajax - asp.net调用

本文关键字:asp net 调用 ajax 方法 服务器端 | 更新日期: 2023-09-27 18:16:36

我正在尝试使用ajax调用c#方法,如下所示。

<a href="#divrecentQ" id="linkdivrecentQ" onclick="lnkClick();" aria-controls="divrecentQ" role="tab" data-toggle="tab">Click here</a>

这是JS中的方法

    function lnkClick() {
        alert("called");
        $.ajax({
            type: "POST",
            url: '/amain.aspx/LoadImages',
            data: {},
            success: function () {
                alert(1);
            },
            dataType: 'html'
        });
        alert("cal");
    }
服务器端:

    public static void LoadImages()
    {
       
        log.Debug("LoadImages is called");
       
    }

服务器端方法没有被调用。

有人能帮帮我吗?

谢谢

服务器端c#方法没有从ajax - asp.net调用

您应该定义static方法并使用[WebMethod]属性包装它。

[WebMethod]
public static void LoadImages()
{
    Label1.Text = "hi therre";
    Response.Redirect("www.google.com");
    log.Debug("LoadImages is called");
}

添加contentType:"application/json;Charset =utf-8","在jquery中调用服务器端方法。

谢谢大家的帮助。:)