ajax调用c#后台方法

-一个大坑 2017-03-20 06:59:24
<script type="text/javascript">
function search() {
var txt = "";
var idcard = $("IDCardtxt").val();//取文本框的值
$.ajax({
type: "post",
url: "WebForm.aspx/IDCard",
data: "{id:" + IDCardtxt + "}",
dataType: "json",
success: function (data) {
if (data != '') { txt = data.d; }
}
})
if (txt != "") { alert(txt); }//如果不为空就弹出后台的字
}
</script>

[WebMethod]
public string IDCard(string txt)
{
if(txt!="")
{
return "有此人";
}
return "";
}
...全文
380 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
-一个大坑 2017-03-21
  • 打赏
  • 举报
回复
[WebMethod]
public static string IDCard_button(string id)
{
if (id != string.Empty)
{
string strSql = "select * from BLACKLIST where id_card=:id_card";
Hashtable hs = new Hashtable();
hs.Add(":id_card", id);
DataTable dt = DataAccessOracle.GetDataTable(strConn, strSql, hs);
if (dt.Rows.Count > 0)
{
String cname = dt.Rows[0]["cname"].ToString().Trim();
return "此人(" + cname + ")不受欢迎";
}
}
return "";
}
后台,用来监视来的人是不是黑名单的人
-一个大坑 2017-03-21
  • 打赏
  • 举报
回复
贴一下完整的代码,前台
function Idcard() {
var mesg = "";
var txtIDCard = $("#txtIDNumber_Cons").val();
$.ajax({
type: "post",
url: "ConstructionInfo.aspx/IDCard_button",
data: "{id:" + txtIDCard + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data != '') {
mesg = data.d;
}
}
})

if (mesg != "") {
alert(mesg)
return;
}

<asp:TextBox ID="txtIDNumber_Cons" runat="server" Text="" Width="150px" CssClass="textstyle"></asp:TextBox>
<input type="button" runat="server" value="讀證件信息" onmousedown="Idcard();" id="btnReadIdName"
style="width: 85px" />
-一个大坑 2017-03-21
  • 打赏
  • 举报
回复
引用 6 楼 ducker3590 的回复:
[quote=引用 5 楼 happy4944 的回复:]
[quote=引用 4 楼 ducker3590 的回复:]
<script type="text/javascript">
function search() {
var txt = "";
var idcard = $("IDCardtxt").val();//取文本框的值 //这样确定能取到值?
$.ajax({
type: "post",
url: "WebForm.aspx/IDCard",//WebForm.aspx.cs的IDCard方法
data: "{id:" + IDCardtxt + "}",//赋值给id传后台查询 //这里应该用idcard
dataType: "json",
success: function (data) {
if (data != '') { txt = data.d; }
}
})
if (txt != "") { alert(txt); }//如果不为空就弹出有此人
}
</script>

那怎么取值[/quote]

如果IDCardtxt是元素的id的话 $("#IDCardtxt").val() 要这样写[/quote]
改了后台还是没有返回值,调试ajax能不能调用后台方法
csdnFUCKINGSUCKS 2017-03-20
  • 打赏
  • 举报
回复
引用 5 楼 happy4944 的回复:
[quote=引用 4 楼 ducker3590 的回复:] <script type="text/javascript"> function search() { var txt = ""; var idcard = $("IDCardtxt").val();//取文本框的值 //这样确定能取到值? $.ajax({ type: "post", url: "WebForm.aspx/IDCard",//WebForm.aspx.cs的IDCard方法 data: "{id:" + IDCardtxt + "}",//赋值给id传后台查询 //这里应该用idcard dataType: "json", success: function (data) { if (data != '') { txt = data.d; } } }) if (txt != "") { alert(txt); }//如果不为空就弹出有此人 } </script>
那怎么取值[/quote] 如果IDCardtxt是元素的id的话 $("#IDCardtxt").val() 要这样写
-一个大坑 2017-03-20
  • 打赏
  • 举报
回复
引用 4 楼 ducker3590 的回复:
<script type="text/javascript"> function search() { var txt = ""; var idcard = $("IDCardtxt").val();//取文本框的值 //这样确定能取到值? $.ajax({ type: "post", url: "WebForm.aspx/IDCard",//WebForm.aspx.cs的IDCard方法 data: "{id:" + IDCardtxt + "}",//赋值给id传后台查询 //这里应该用idcard dataType: "json", success: function (data) { if (data != '') { txt = data.d; } } }) if (txt != "") { alert(txt); }//如果不为空就弹出有此人 } </script>
那怎么取值
csdnFUCKINGSUCKS 2017-03-20
  • 打赏
  • 举报
回复
<script type="text/javascript"> function search() { var txt = ""; var idcard = $("IDCardtxt").val();//取文本框的值 //这样确定能取到值? $.ajax({ type: "post", url: "WebForm.aspx/IDCard",//WebForm.aspx.cs的IDCard方法 data: "{id:" + IDCardtxt + "}",//赋值给id传后台查询 //这里应该用idcard dataType: "json", success: function (data) { if (data != '') { txt = data.d; } } }) if (txt != "") { alert(txt); }//如果不为空就弹出有此人 } </script>
-一个大坑 2017-03-20
  • 打赏
  • 举报
回复
引用 1 楼 happy4944 的回复:
感觉没进入后台方法,txt的值一直为空
我打断点也进不去后台方法,不知道是ajax本身调试进不去,还是我写错了进不去
-一个大坑 2017-03-20
  • 打赏
  • 举报
回复
上面好像写错了 <script type="text/javascript"> function search() { var txt = ""; var idcard = $("IDCardtxt").val();//取文本框的值 $.ajax({ type: "post", url: "WebForm.aspx/IDCard",//WebForm.aspx.cs的IDCard方法 data: "{id:" + IDCardtxt + "}",//赋值给id传后台查询 dataType: "json", success: function (data) { if (data != '') { txt = data.d; } } }) if (txt != "") { alert(txt); }//如果不为空就弹出有此人 } </script> [WebMethod] public string IDCard(string id) { if(查询id有没有) { return "有此人"; } return ""; }
-一个大坑 2017-03-20
  • 打赏
  • 举报
回复
感觉没进入后台方法,txt的值一直为空

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧