【问题标题】:2 page methods on an aspx pageaspx 页面上的 2 个页面方法
【发布时间】:2011-06-02 01:05:24
【问题描述】:

我正在用 jquery 调用一个页面方法,它工作得很好。我正在创建第二个,但它根本不起作用;我得到的只是错误函数。是否可以在一个 aspx 页面中放置超过 1 个页面方法?

这是我在客户端上的 jquery:

function LoadCount() {

    var TheObject = $.toJSON(CurrentForm);
    var TheParameter = "{'TheParameter' : '" + TheObject + "'}";

    $('#testobj').html("loading");

    $.ajax({
        type: "POST",
        url: "../Pages/MyPage.aspx/GetCount",
        data: TheParameter,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: successFn,
        error: errorFn
    });

};

function successFn(thedata) {   $('#result').html(thedata.d); };
function errorFn() { alert("problem getting count"); };

function LoadData() {

    var ConfirmLoad = "test";

    $.ajax({
        type: "POST",
        url: "../Pages/MyPage.aspx/GetLoaded",
        data: ConfirmLoad,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: successLoad,
        error: errorLoad
    });

};

function successLoad(thedata) { alert((thedata.d)); };
function errorLoad() { alert("problem getting loaded"); };

在服务器端,我有这个:

[WebMethod]
public static string GetCount(string TheParameter)
{
  // some code
  return JsonResult;
}

[WebMethod]
public static string GetLoaded(string ConfirmLoad)
{
  return "test string";
}

LoadCount 和 GetCount 工作得很好,我想我会复制实现来创建另一个页面方法,但是第二次,没有什么好的事情发生。感谢您的建议。

【问题讨论】:

  • 可以在 aspx.cs|vb 文件中定义多个方法,如果这是您所要求的。包含几个代码 sn-ps 说明您已完成的工作、您期望发生的事情以及实际发生的事情会很有帮助。
  • @Pete,刚刚添加了代码示例。
  • 首先,我需要找到一种方法,在我的代码中使用一个名为 GetLoaded() 的函数。其次,代码示例很好,但如果没有更多关于按什么顺序发生的详细信息,仍然很难为您提供帮助。至少请包括您收到的详细例外情况。您是否尝试过在函数上设置断点并查看它们是否被调用?
  • 将数据类型从 json 更改为 text 时出现问题。
  • 酷,很高兴 SirViver 帮您解决了问题!

标签: asp.net


【解决方案1】:

如果您只是返回纯文本而不是 JSON 编码字符串,则需要在 $.ajax() 调用属性中设置 dataType: "text"

如果您发送纯文本而不是 JS 对象,您可能还希望不指定 contentType(默认值为 'application/x-www-form-urlencoded')。

【讨论】:

    猜你喜欢
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    相关资源
    最近更新 更多