在ASPX页面中使用webmethod服务AJAX请求。
16lz
2021-01-22
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?
我正在尝试使用.aspx页面中的一个方法来服务AJAX请求。由于某些原因,我没有返回我想要的数据。有人能告诉我我做错了什么吗?
mypage.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:
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:
当我点击mybutton时,我得到一个警告“错误:”然后很多HTML都说:
Unknown web method testmethod.
Parameter name: methodName
2 个解决方案
#1
12
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.
另外,如果不使用代码隐藏文件,我不确定是否支持页方法。
更多相关文章
- 如何使用ajax GET或POST方法将数据传递到amazon lambda节点。js
- Jquery常用技巧和方法收集
- Chosen 基于jquery的选择框插件使用方法
- jQuery Ui Draggable在移动端浏览器不起作用解决方案
- jQuery $ajax 传中文数据到action乱码解决方法
- JQuery 的选取元素 和 方法
- jquery的function方法中each循环里面判断失败跳出function方法
- js/jquery常用方法------复制粘贴至剪切板
- Jquery ready和window.onload方法区别