我正在尝试使用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");
}
服务器端方法未被调用。
有人可以帮忙吗?
由于
答案 0 :(得分:2)
您应该定义静态方法并使用[WebMethod]
属性将其包装起来。
[WebMethod]
public static void LoadImages()
{
Label1.Text = "hi therre";
Response.Redirect("www.google.com");
log.Debug("LoadImages is called");
}
答案 1 :(得分:0)
添加 “contentType:”application / json; charset = utf-8“,”在jquery中称为服务器端方法。
感谢大家的帮助。 :)