I am trying to service AJAX requests with a method in my .aspx page. For some reason I am not getting the data returned that I want. Can anybody tell me what I am doing wrong?
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));
}
});
});
When I click mybutton
I get an alert "error:" and then whole lot of HTML that says:
Unknown web method testmethod.
Parameter name: methodName
webMethods Service Development Help includes this Working with Web Services topic which provides procedures for using Designer to create web service descriptors, adding operations, binders, handlers, and policies to a web service descriptor; and setting web service descriptor properties.
Ajax. Ajax is the traditional way to make an asynchronous HTTP request. Data can be sent using the HTTP POST method and received using the HTTP GET method. To make an HTTP call in Ajax, you need to initialize a new XMLHttpRequest() method, specify the URL endpoint and HTTP method (in this case GET).
The method needs to be Shared
:
<WebMethod()> Public Shared Function testmethod() As Integer
Return 5
End Function
Also, I'm not sure that page methods are supported when you don't use a code-behind file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With