【发布时间】: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