jquery.ajax 正确获取后台返回的文本
测试代码:functionuser_isexist(name){$.ajax({url:"user_check.aspx?name="+name,type:"GET",a...
测试代码:
function user_isexist(name)
{
$.ajax({url:"user_check.aspx?name="+name,type:"GET",async:true,dataType:"text",success: function(text){
alert(text==name);
}
后台代码:
string name =Request.QueryString["name"].ToString();
Response.write(name);
为什么alert(text==name);总是false?那应该怎样获取后台文本 展开
function user_isexist(name)
{
$.ajax({url:"user_check.aspx?name="+name,type:"GET",async:true,dataType:"text",success: function(text){
alert(text==name);
}
后台代码:
string name =Request.QueryString["name"].ToString();
Response.write(name);
为什么alert(text==name);总是false?那应该怎样获取后台文本 展开
2个回答
展开全部
哥们刚研究出来的给你个例子《jquery 的AJAX运用》
页面代码:
============================================
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<script type="text/javascript">
$(function(){
$("#button2").click(function(){
$.post("Default2.aspx",{aa:"aa1",bb:"bb1"},
function(data){
$("#Label1").html(data.Id);
$("#Label2").html(data.Name);
$("#Label3").html(data.Aa);
$("#Label4").html(data.Bb);
},"json"
)
})
})
</script>
id:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
name:<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
aa:<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />
bb:<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br />
<input type="Button" id="button2" value="获取" />
</div>
</form>
</body>
</html>
----------------------------------------------
后台代码:
----------------------------------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Script.Serialization;//需要导入这个包
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//获取提交的数据方法
string a = Request["aa"].ToString() + "返回值";
string b = Request["bb"].ToString() + "返回值";
info info1 = new info();
info1.Id = "info_id";
info1.Aa = "info_aa"+a;
info1.Bb = "info_bb"+b;
//实体类转换JSON格式数据
Response.Write(ToJSON(info1));
}
//实体类转换JSON格式数据 方法
public static string ToJSON(object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
页面代码:
============================================
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<script type="text/javascript">
$(function(){
$("#button2").click(function(){
$.post("Default2.aspx",{aa:"aa1",bb:"bb1"},
function(data){
$("#Label1").html(data.Id);
$("#Label2").html(data.Name);
$("#Label3").html(data.Aa);
$("#Label4").html(data.Bb);
},"json"
)
})
})
</script>
id:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
name:<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
aa:<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />
bb:<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br />
<input type="Button" id="button2" value="获取" />
</div>
</form>
</body>
</html>
----------------------------------------------
后台代码:
----------------------------------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Script.Serialization;//需要导入这个包
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//获取提交的数据方法
string a = Request["aa"].ToString() + "返回值";
string b = Request["bb"].ToString() + "返回值";
info info1 = new info();
info1.Id = "info_id";
info1.Aa = "info_aa"+a;
info1.Bb = "info_bb"+b;
//实体类转换JSON格式数据
Response.Write(ToJSON(info1));
}
//实体类转换JSON格式数据 方法
public static string ToJSON(object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-07-27
展开全部
首先你的 ,js 有语法错误 ,后面的括号没有闭合,我帮你写一个 完整的吧:
<script type="text/javascript">
function user_isexist(name){
$.get("user_check.aspx?name="+name,function(text){
alert(text);
alert(text==name);
},'text');
}
</script>
有问题,追问,
<script type="text/javascript">
function user_isexist(name){
$.get("user_check.aspx?name="+name,function(text){
alert(text);
alert(text==name);
},'text');
}
</script>
有问题,追问,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询