Próbuję obsługiwać żądania AJAX za pomocą metody na mojej stronie .aspx. Z jakiegoś powodu nie otrzymuję danych, które chcę odzyskać. Czy ktoś może mi powiedzieć, co robię źle?Żądania usługi AJAX z webmethod na stronie ASPX
mypage.aspx:
<%@ Page Language="VB" Title="My Page" %>
<%@ Import Namespace="System.Web.Services" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(sender As Object, e As System.EventArgs)
End Sub
<WebMethod()> Public Function testmethod() As Integer
Return 5
End Function
</script>
<html>
<!--...rest of page including mybutton and myresults-->
JQuery:
$("#mybutton").click(function() {
$.ajax({
type: "POST",
url: "mypage.aspx/testmethod",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert("success");
$("#myresults").html(msg.d);
},
error: function(msg) {
alert("error:" + JSON.stringify(msg));
}
});
});
Kiedy klikam mybutton
otrzymuję alert "Błąd:", a następnie całe mnóstwo HTML, który mówi:
Unknown web method testmethod.
Parameter name: methodName
Dzięki, to rozwiązało problem. – Flash
Aby dodać - W języku C# - musi to być "static" – Lijo