Javascript 在C#codebehind中调用WebMethod,而不使用ASPX页面中的服务器表单

Javascript 在C#codebehind中调用WebMethod,而不使用ASPX页面中的服务器表单,javascript,c#,jquery,asp.net,ajax,Javascript,C#,Jquery,Asp.net,Ajax,由于样式的问题,并且需要在一个网页上有多个表单,我目前有一个表单超出了runat=server的正常表单 我是否仍然可以使用ajax在该网页的C#codebehind中调用WebMethod?我想以不同的形式将此表单中的信息提交到我在页面前面使用的相同连接字符串中 这是我目前的代码: $().ready(function () { $("input[type=submit]").click(function () { handleClick(); crea

由于样式的问题,并且需要在一个网页上有多个表单,我目前有一个表单超出了runat=server的正常表单

我是否仍然可以使用ajax在该网页的C#codebehind中调用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");
    }
});
});
以及我在codebehind中的WebMethod:

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

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

您需要将该方法声明为静态

[WebMethod]
public static string CreateJob()
       ^^^^^
{
    //rest of my database code here
}
另一个问题是,如果
input[type=submit]
是ASP.Net按钮控件,它将发回服务器不能使用ASP.Net服务器控件进行jQuery Ajax调用-$.Ajax。


您需要使用常规html输入按钮控件和
type=button
而不是
type=submit
webmethod应该是静态的

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

CreateJob方法属于哪个类?ASP.NET中web方法的URL路径约定为
Class/Method
。它是。使用开发人员工具在“正常”上下文中检查WebMethod请求。用AJAX做同样的事情。如果出现错误,则请求未被正确模拟。我打赌您得到的是404响应,“Account/help.aspx/CreateJob”未解析到您认为它正在解析的位置。@maniak1982它是一个公共部分类gethelp,继承自Page类,然后是指向my Page help.aspx.cs的aspx端链接,但也继承自gethelp类…可能是我有一个名为help的页面,而他们继承自一个搞砸了的gethelp类吗?(即更改所有内容的名称为相同)@user2864740谢谢!在哪里可以找到开发人员工具?或者有没有关于如何做到这一点的快速教程?我以前从来没用过这个把戏谢谢!还有一个简单的问题,我以前的codebehind不是静态的,因为登录时会记录并提交用户的姓名。有没有办法在保持WebMethod静态的同时仍然允许记录用户名?谢谢,我不确定我是否理解您的评论,但是,如果您需要会话,您将需要
[WebMethod(EnableSession=true)]
。如果没有,您能否创建一个新问题?@StevenPfeifer会话瞬态数据应通过会话状态共享。每个响应之后都没有页面的运行实例:它是为每个请求创建和销毁的。在某些情况下,使用静态变量可能看起来“有效”,但这种方法不可靠(例如,无法跨appdomain工作),并且容易在交叉请求之间保持无效状态。@StevenPfeifer“重新验证用户?”如果用户经过身份验证,会话可用于存储-通常会存储类似“用户id”的内容,其中,只有经过身份验证/登录/有效的用户才具有这样的设置。这可以在WebMethod中与传输的所有其他会话信息一起检查。WebMethod本身与身份验证无关。会话状态通常被认为是“安全的”;假设会话当前cookie未被拦截/窃取(请参阅CSRF等,了解相关的其他预防措施)。@StevenPfeifer会话状态使用的基本预防措施是仅http cookie(.NET内置会话状态提供程序中的默认值)和HTTPS,等等。
[WebMethod]
public static string CreateJob()
{
    //rest of my database code here
}