【发布时间】:2021-12-19 04:11:21
【问题描述】:
我正在使用 ASP.NET Web 表单技术和 jquery ajax 处理这个示例场景: 在输入文本元素上的更改事件中,必须将 ajax 请求发送到 asp.net 页面(Login.aspx/GetDoublicate)后面的代码中的函数,以检查数据库中是否存在电子邮件并返回 true 或 false。 我的代码:
<form id="form1" runat="server">
<div>
<table style="width:100%;" dir="rtl">
<tr>
<td class="auto-style1">user name</td>
<td class="auto-style1">
<input id="Text1" type="text" /></td>
<td class="auto-style1"></td>
</tr>
<tr>
<td class="auto-style1">password</td>
<td class="auto-style1">
<input id="Password1" type="password" /></td>
<td class="auto-style1"></td>
</tr>
<tr>
<td class="auto-style1">
confirm password</td>
<td class="auto-style1">
<input id="Password2" type="password" /></td>
<td class="auto-style1"></td>
</tr>
<tr>
<td>
email</td>
<td>
<input id="Text2" runat="server" type="email" /></td>
<td> </td>
</tr>
<tr>
<td>
birth</td>
<td>
<input id="Text3" type="date" /></td>
<td> </td>
</tr>
<tr>
<td>
<input id="Button1" type="submit" value="Subscripe" /></td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
</form>
<div id="fffg">
</div>
ajax 请求代码
<script>
$(document).ready(function () {
$('#Text2').change(function () {
$.ajax({
type: "GET",
url: "Login.aspx/GetDoublicate",
'data': {"email":$('#Text2').val() },
//contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (response) {
console.log(response);
}
});
})
})
</script>
Login.aspx页面后面代码:
public bool GetDoublicate()
{
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string sqltext = "select id from CoAuthor where email='" + Request.Params["email"] + "'";
SqlCommand cmd = new SqlCommand(sqltext, con);
string x = cmd.ExecuteScalar().ToString();
con.Close();
if (string.IsNullOrEmpty(x))
{
return true;
}
else return false;
}
之后我得到这个: result
在使用控制台记录响应后,我的页面整个元素不仅打印了 true 或 false,这意味着我不需要成功调用函数。
我尝试使用 WebMethod 装饰但同样的失败结果指出我需要从静态方法无法做到的数据库中获取数据。
我尝试使用更新面板并将隐藏的 ASP 按钮放入其中,因此当(在 Text2 上发生更改事件)我使用 jquery .click 方法单击隐藏按钮但我也无法获得任何结果。
提前感谢大家。
【问题讨论】:
-
在调试模式下,您可以在
GetDoublicate()中插入断点。跑步会停止吗?可以在<input id="Text2" runat="server" type="email" /><input id="Text2" runat="server" type="email" />上查看网页返回的代码吗?