【问题标题】:Call WebMethod in C# codebehind without using a server form in ASPX page在 C# 代码隐藏中调用 WebMethod,而不在 ASPX 页面中使用服务器表单
【发布时间】:2017-01-18 19:37:00
【问题描述】:

由于样式问题,并且需要在一个网页上使用多个表单,我目前有一个表单,它不是带有 runat=server 的正常表单。

我是否仍然可以使用 ajax 在此网页的 C# 代码隐藏中调用 WebMethod?我想将此表单中的信息以不同的形式提交到我在页面前面使用的相同连接字符串。

这是我当前的代码:

$().ready(function () {
    $("input[type=submit]").click(function () {
        handleClick();
        createJob();
    });
});

function createJob() {
var txtTestValue = document.getElementById('jobTitle').value;

$.ajax({
    // POST signals a data request
    type: "POST",
    // This directs which function in the c# code behind to use
    url: "Account/help.aspx/CreateJob",
    // The paramater pageIndex, page number we need to load, to pass to GetCustomers(int pageIndex)
    data: txtTestValue,
    // Type of data we are sending to the server (i.e. the pageIndex paramater)
    contentType: "application/json; charset=utf-8",
    // Type of data we expect back from the server (to fill into the html ultimately)
    dataType: "text",
    // If all goes smoothly to here, run the function that fills our html table
    success: OnSuccess,
    // On failure, error alert user (aka me so that I know something isn't working)
    failure: function (response) {
        alert("failure");
    },
    error: function (response) {
        alert("error");
    }
});
});

还有我在代码隐藏中的 WebMethod:

[WebMethod]
public string CreateJob()
{
    //rest of my database code here
}

很抱歉造成混淆,但它在 ajax 代码之前一直在做所有事情,然后似乎忽略了它(并返回 ajax 失败并出现错误)。我的代码没有到达 WebMethod,并且我在 Visual Studio 中设置的任何断点都不会在页眉中触发。提前感谢您的帮助!

【问题讨论】:

  • CreateJob 方法属于哪个类? ASP.NET 中 Web 方法的 URL 路径约定为 Class/Method
  • 是的。使用开发人员工具在“正常”上下文中检查 WebMethod 请求。用 AJAX 做同样的事情。如果出现错误,则未正确模拟请求。
  • 我敢打赌,您收到的是 404 响应,并且“Account/help.aspx/CreateJob”没有解决到您认为解决的位置。
  • @maniak1982 它是一个公共的局部类gethelp,它继承自Page类,然后aspx侧链接到我的页面help.aspx.cs,但也继承自gethelp类...可以吗是我有一个名为帮助的页面,他们从一个搞砸的 gethelp 类继承? (即,将所有内容的名称更改为相同)
  • @user2864740 谢谢!我在哪里可以找到开发人员工具?或者是否有关于如何做到这一点的快速教程?我以前从未使用过这些

标签: javascript c# jquery asp.net ajax


【解决方案1】:

您需要将方法声明为静态

[WebMethod]
public static string CreateJob()
       ^^^^^
{
    //rest of my database code here
}

另一个问题是如果input[type=submit] 是 ASP.Net 按钮控件,它会回发到服务器。 您不能使用 ASP.Net 服务器控件进行 jQuery Ajax 调用 - $.ajax。

// This code won't work if `input[type=submit]` is a server button control
$(function () {
    $("input[type=submit]").click(function () {
        handleClick();
        createJob();
    });
});

您需要使用带有type=button 而不是type=submit 的常规html 输入按钮 控件.

【讨论】:

  • 成功了,谢谢!另一个快速的问题,我之前的代码隐藏不是静态的,因为登录时会记录并提交用户名。有没有办法解决这个问题,仍然允许记录用户名,同时保持 WebMethod 静态?再次感谢
  • 我不确定我是否理解您的评论,但是,如果您需要会话,则需要[WebMethod(EnableSession = true)]。如果没有,您能创建一个新问题吗?
  • @StevenPfeifer 会话瞬态数据应通过会话状态共享。每次响应后都有 no 正在运行的 Page 实例:它为每个请求创建和销毁。在某些情况下,使用静态变量可能看起来“有效”,但这种方法不可靠(例如,无法跨应用程序域工作)并且容易在交错请求之间保持无效状态。
  • @StevenPfeifer "重新认证用户?" Session 可用于存储用户是否已通过身份验证-通常会存储诸如“用户 ID”之类的内容,其中只有经过身份验证/登录/有效的用户具有此类设置。这可以在 WebMethod 中与传输的所有其他会话信息一起检查。 WebMethod 本身对身份验证没有任何作用。会话状态通常被认为是“安全的”;假设会话 nonce-cookie 没有被拦截/窃取(有关相关的额外预防措施,请参阅 CSRF 等)。
  • @StevenPfeifer 会话状态使用的基本预防措施是仅 http cookie(.NET 内置会话状态提供程序中的默认设置)和 HTTPS,等等。
【解决方案2】:

网络方法应该是静态的。

[WebMethod]
public static string CreateJob()
{
    //rest of my database code here
}

【讨论】:

    猜你喜欢
    • 2012-02-16
    • 2018-02-16
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 2012-09-27
    • 1970-01-01
    相关资源
    最近更新 更多