简体   繁体   English

在ASPX页面中使用webmethod服务AJAX请求

[英]Service AJAX requests with webmethod in ASPX page

I am trying to service AJAX requests with a method in my .aspx page. 我正在尝试使用.aspx页面中的方法为AJAX请求提供服务。 For some reason I am not getting the data returned that I want. 出于某种原因,我没有得到我想要的数据。 Can anybody tell me what I am doing wrong? 谁能告诉我我做错了什么?

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 

The method needs to be Shared : 该方法需要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. 另外,当您不使用代码隐藏文件时,我不确定是否支持页面方法。

What is the error that is being passed back?? 传回的错误是什么? The reason you are seeing the whole html page is because you have an error in the code and the msg that is being passed back is the full html which tells you where the error is. 您看到整个html页面的原因是因为您在代码中有错误并且正在传回的消息是完整的html,它告诉您错误的位置。 I'm sure if you fix the error then you would be fine. 我确定如果你修复错误然后你会没事的。

I'm not sure if you are expecting to see the error message from the codebehind. 我不确定您是否期望从代码隐藏中看到错误消息。 Remember - the success or error functions being called in the Ajax section are called depending on whether the webservice method was called successfully or not. 请记住 - 根据Web服务方法是否成功调用,调用Ajax部分中调用的成功或错误函数。 I think you are thinking that you will see the error message from teh webmethod here, but in reality, if the webmethod is throwing the error then you would still have a successful ajax call and thus the "success" function would be the one running. 我认为你认为你会在这里看到来自webmethod的错误消息,但实际上,如果webmethod抛出了错误,那么你仍然会有一个成功的ajax调用,因此“成功”函数就是运行的那个。 The "error" function will only run when the whole webmethod call fails, so there is no response from the server. “错误”功能仅在整个webmethod调用失败时运行,因此服务器没有响应。

Hope that makes sense. 希望有道理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM