【发布时间】:2019-09-13 06:44:18
【问题描述】:
我正在为我的办公室创建一个医院 ERP 项目。我已经添加了一些功能。在这个时候,我的项目需要 jquery ajax 请求。我正在尝试更多,但我在这个网络表单中并不成功。所以我需要帮助专家。 我的代码不起作用 asp.net webform jquery ajax 请求。
这是我的代码
[System.Web.Services.WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Employee GetByEmployeeId(int Id)
{
Employee employee = new Employee();
return employee;
}
<script src="Scripts/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#saveButton').click(function () {
debugger;
$.ajax({
type: 'POST',
url: 'Test.aspx/GetByEmployeeId',
data: '{Id: ' + 1 + '}',
success: function (respons) {
debugger;
alert('ok');
},
error: function () {
alert('fail')
}
})
})
})
</script>
<div class="form-group">
<asp:Label runat="server" For="nametextBox">Name:</asp:Label>
<asp:TextBox runat="server" ID="nametextBox" CssClass="form-control">
</asp:TextBox>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" id="saveButton">Save</button>
</div>
我正在尝试,但没有工作
【问题讨论】:
-
它在哪些方面不适合您?
-
是的,它不工作
-
将
GetByEmployeeId设为static。阅读 here 了解为什么WebMethod被标记为静态 -
我同意@Izzy。使您的 webmethod 静态并传递您需要的字段作为参数。
-
您还必须在返回对象之前对其进行序列化。