c# - 使用ajax调用asp.net web表单代码
问题描述
我正在使用 ASP.NET Web 表单技术和 jquery ajax 处理此示例场景:在输入文本元素上的更改事件中,必须将 ajax 请求发送到 asp.net 页面(Login.aspx/GetDoublicate)后面的代码中的函数到检查数据库中是否存在电子邮件并返回真或假。我的代码:
<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;
}
之后我得到这个: 结果
在使用控制台记录响应后,我的页面整个元素不仅打印了 true 或 false,这意味着我不需要成功调用该函数。
我尝试使用 WebMethod 装饰但同样的失败结果指出我需要从静态方法无法做到的数据库中获取数据。
我尝试使用更新面板并在其中放置隐藏的 ASP 按钮,因此当(更改事件发生在 Text2 上)我使用 jquery .click 方法单击隐藏按钮但我也无法获得任何结果。
提前感谢大家。
解决方案
经过数小时的尝试和研究,我找到了解决方案,这是我的完整代码:
$(document).ready(function () {
$('#Text2').change(function () {
var ema = $('#Text2').val();
$.ajax({
type: "POST",
url: "Login.aspx/GetDoublicate",
// data: '{"email":'+$('#Text2').val()+ ',}',
data: JSON.stringify({ "email":ema }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
// console.log(response.d);
if(response.d == true)
{ alert("doublicate email discovered"); }
else {alert("Ok, go on")};
}
,
error: function (xhr, err) { alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status + "\nresponseText: " + xhr.responseText); }
});
})
})
注意 json 参数必须与称为参数的函数的名称相同。
这里的 asp 代码:[WebMethod] public static bool GetDoublicate(string email) {
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string sqltext = "select id from CoAuthor where email='" + email + "'";
SqlCommand cmd = new SqlCommand(sqltext, con);
SqlDataReader dr= cmd.ExecuteReader();
while (dr.Read())
{
return true;
}
con.Close();
return false;
}
推荐阅读
- julia - 通过具有相同抽象父类型的对象集合保持/循环的最佳实践 (Julia)
- excel - 从数组中返回值大于 0 在下面的数组中
- asp.net - ASP.NET MVC CORE - 多对多关系 - 创建视图
- python - 使用 Django 删除一对多相关模型
- amazon-web-services - Gunicorn Elastic Beanstalk 更改超时
- typescript - 对象文字只能指定已知属性 - 类型“SaveOptions”中不存在
- swift - 每当我推送 ViewController - Swift - 以编程方式时,大量 CPU 过度使用
- java - 以编程方式在 Eclipse PDE 视图中打开编辑器
- css - 更改 FontAwesomeIcon 的大小
- flutter - 如何将参数传递给 FutureBuilder 中的未来属性函数