服务与AJAX请求的WebMethod在ASPX页面 [英] Service AJAX requests with webmethod in ASPX page

查看:335
本文介绍了服务与AJAX请求的WebMethod在ASPX页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想服务,在我的.aspx页面中的方法AJAX请求。出于某种原因,我没有得到返回的数据,我想。谁能告诉我什么,我做错了什么?

mypage.aspx:

 <%@页面语言=VB标题=我的主页%GT;
<%@导入命名空间=System.Web.Services%GT;
<%@导入命名空间=System.Collections.Generic%GT;!< D​​OCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<脚本=服务器>    保护小组的Page_Load(发送者为对象,E作为System.EventArgs)    结束小组    &所述;的WebMethod()>公共职能TestMethod的()作为整数
        返回5
    结束功能< / SCRIPT>< HTML和GT;
< --...页面的其余部分,包括为myButton和myresults - &GT!;

JQuery的:

  $(#myButton的)。点击(函数(){
    $阿贾克斯({
      键入:POST,
      网址:mypage.aspx / TestMethod的
      数据:{},
      的contentType:应用/ JSON的;字符集= UTF-8,
      数据类型:JSON
      成功:函数(MSG){
        警报(成功);
        $(#myresults)HTML(msg.d);
      },
      错误:函数(MSG){
        警报(错误:+ JSON.stringify(MSG));
      }
    });
});

当我点击则myButton 我得到一个警告错误,上面写着,然后一大堆的HTML:

 未知的Web方法TestMethod的。
参数名:方法名


解决方案

的方法必须是共享

 <的WebMethod()GT;公共共享功能TestMethod的()作为整数
    返回5
结束功能

另外,我不知道,当你不使用code隐藏文件页面方法的支持。

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 

解决方案

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请求的WebMethod在ASPX页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆