我现在做的这个例子用text来做出现一个代码漏洞,比如说在txtnames这个文本框数据里面出现一个(,逗号)的时候获取的数据就对应不上了,就乱掉了,就不能给相应的文本框赋上相对的值了。
求高手。
function Check(number) {
var id = number;
var cid = "";
var rid = "";
var lid = "";
var jid = "";
var cid = "";
$.ajax({
type: "POST",
url: "ajax.aspx",
data: 'id='+id+'&cid='+cid+'&rid='+rid+'&lid='+lid+'&jid='+jid,
dataType: "text",
timeout: 10000,
error: function() { alert('服务器出错啦!'); },
success: function(data) {
var splitChar = ',';
var arrResult = data.split(splitChar);
$("#<%=txtid.ClientID %>").val(arrResult[0]);
$("#<%=txtnames.ClientID %>").val(arrResult[1]);
$("#<%=txtjilv.ClientID %>").val(arrResult[2]);
$("#<%=txtphone.ClientID %>").val(arrResult[3]);
$("#<%=txtdiscovertime.ClientID %>").val(arrResult[4]);
$("#<%=txtlinkname.ClientID %>").val(arrResult[5]);
$("#<%=txtstates.ClientID %>").val(arrResult[6]);
$("#<%=txtqiandantime.ClientID %>").val(arrResult[7]);
$("#<%=txtmoney.ClientID %>").val(arrResult[8]);
$("#<%=txtusername.ClientID %>").val(arrResult[9]);
$("#<%=lblfzname.ClientID %>").val(arrResult[10]);
$("#<%=txtcomment.ClientID %>").val(arrResult[11]);
}
});
}
这个是ajax.aspx的后台代码
protected void Page_Load(object sender, EventArgs e)
{
string id = Request["id"].ToString();
string cid = Request["cid"].ToString();
string rid = Request["rid"].ToString();
string lid = Request["lid"].ToString();
string jid = Request["jid"].ToString();
if (id == "")
{
this.ClientString();
}else if(cid == "")
{
this.ChanceString();
}
else if (rid == "")
{
this.CalendarString();
}
else if (lid == "")
{
this.LinkmanString();
}
else if (jid == "")
{
this.PlanString();
}
else
{
return;
}
}
//销售机会
private void ChanceString()
{
string id = Request["id"].ToString();
HttpCookie cookie = Session["adminsession"] as HttpCookie;
string names = cookie["admingongsi"].ToString();
string username = cookie["adminname"].ToString();
string types = cookie["admintypes"].ToString();
SqlConnection con = new SqlConnection(strConn);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Chance where id='"+id+"' and Cname='"+names+"'",con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string idtwo = dr["id"].ToString();
string namestwo = dr["names"].ToString();
string jilv = dr["jilv"].ToString();
string phone = dr["phone"].ToString();
string discovertime = dr["discovertime"].ToString();
string linkname = dr["linkname"].ToString();
string states = dr["states"].ToString();
string qiandantime = dr["qiandantime"].ToString();
string money = dr["money"].ToString();
string usernametwo = dr["username"].ToString();
string fzname = dr["fzname"].ToString();
string zt = "";
string comment = dr["comment"].ToString();
if (states == "../images/ok.gif")
{
zt = "有效";
}
else
{
zt = "无效";
}
con.Close();
string strReturn = string.Empty;
strReturn = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}",
idtwo,
namestwo,
jilv,
phone,
discovertime,
linkname,
zt,
qiandantime,
money,
usernametwo,
fzname,
comment);
//Response.Write(idtwo,namestwo,jilv , phone , discovertime , linkname , zt , qiandantime , money , usernametwo , fzname);
Response.Write(strReturn);
Response.End();
}
}
16 个解决方案
#1
JavaScriptSerializer
DataContractJsonSerializer
string str = "{\"root\":{\"row\":[{\"ecode\":\"1\",\"emsg\":\"ok\"},{\"ecode\":\"2\",\"emsg\":\"no\"}]}}";
JavaScriptObject obj = (JavaScriptObject)JavaScriptConvert.DeserializeObject(str);
if (obj.Count > 0)
{
JavaScriptObject ob = (JavaScriptObject)obj["root"];
JavaScriptArray array = (JavaScriptArray)ob["row"];
if (array.Count > 0)
{
foreach (JavaScriptObject o in array)
{
Response.Write(o["ecode"] + "===" + o["emsg"] + "<br/>");
}
}
}
DataContractJsonSerializer
string str = "{\"root\":{\"row\":[{\"ecode\":\"1\",\"emsg\":\"ok\"},{\"ecode\":\"2\",\"emsg\":\"no\"}]}}";
JavaScriptObject obj = (JavaScriptObject)JavaScriptConvert.DeserializeObject(str);
if (obj.Count > 0)
{
JavaScriptObject ob = (JavaScriptObject)obj["root"];
JavaScriptArray array = (JavaScriptArray)ob["row"];
if (array.Count > 0)
{
foreach (JavaScriptObject o in array)
{
Response.Write(o["ecode"] + "===" + o["emsg"] + "<br/>");
}
}
}
#2
dataType改为json
Response.Write(string.format("{\"ID\":\"{0}\",\"Name\":\"{1}\"}",xx,xx));
//输出类型改为json
success:function(result){
var id=result.ID;
var name=result.Name;
}
//取值改为这样取
#3
谢谢楼上的回答
那么那个数据库查询那些逻辑写到什么位置呢?怎么写呢?
#4
你原来怎么写就怎么写,只是最后拼接成json格式的,最后Response.Write输出
#5
也就是说我那个ajax.aspx.cs里面的代码不用修改是吗?
#6
Response.Write(string.format("{\"ID\":\"{0}\",\"Name\":\"{1}\"}",xx,xx));
你这个错误了哦,格式不正确,,,怎么写?
#7
format要大写,我是手写的没注意。
#8
Response.Write(string.Format("{\"idtwo\":\"{0}\",\"namestwo\":\"{1}\",\"jilv\":\"{2}\",\"phone\":\"{3}\",\"discovertime\":\"{4}\",\"linkname\":\"{5}\",\"zt\":\"{6}\",\"qiandantime\":\"{7}\",\"money\":\"{8}\",\"usernametwo\":\"{9}\",\"fzname\":\"{10}\",\"comment\":\"{11}\"}",idtwo,namestwo,jilv,phone,discovertime,linkname,zt,qiandantime, money, usernametwo,fzname,comment));
嗯嗯,但是还是提示错误了,说格式问题方便的话加下我QQ806778705
#9
Response.Write(string.Format("{\"idtwo\":\"{0}\",\"namestwo\":\"{1}\",\"jilv\":\"{2}\",\"phone\":\"{3}\",\"discovertime\":\"{4}\",\"linkname\":\"{5}\",\"zt\":\"{6}\",\"qiandantime\":\"{7}\",\"money\":\"{8}\",\"usernametwo\":\"{9}\",\"fzname\":\"{10}\",\"comment\":\"{11}\"}",idtwo,namestwo,jilv,phone,discovertime,linkname,zt,qiandantime, money, usernametwo,fzname,comment));
//提示我格式不正确....怎么回事
#10
你确定是这一句问题?
你把全部代码都帖上来看看!
你把全部代码都帖上来看看!
#11
好的我都贴出来吧。
function Check(number) {
var id = number;
var cid = "";
var rid = "";
var lid = "";
var jid = "";
var cid = "";
$.ajax({
type: "POST",
url: "ajax.aspx",
data: 'id='+id+'&cid='+cid+'&rid='+rid+'&lid='+lid+'&jid='+jid,
dataType: "json",
timeout: 10000,
error: function() { alert('服务器出错啦!'); },
success: function(data) {
var idtwo = data.idtwo;
var namestwo = data.namestwo;
var jilv = data.jilv;
var phone = data.phone;
var discovertime = data.discovertime;
var linkname = data.linkname;
var zt = data.zt;
var qiandantime = data.qiandantime;
var money = data.money;
var usernametwo = data.usernametwo;
var fzname = data.fzname;
var comment = data.comment;
$("#<%=txtid.ClientID %>").val(idtwo);
$("#<%=txtnames.ClientID %>").val(namestwo);
$("#<%=txtjilv.ClientID %>").val(jilv);
$("#<%=txtphone.ClientID %>").val(phone);
$("#<%=txtdiscovertime.ClientID %>").val(discovertime);
$("#<%=txtlinkname.ClientID %>").val(linkname);
$("#<%=txtstates.ClientID %>").val(zt);
$("#<%=txtqiandantime.ClientID %>").val(qiandantime);
$("#<%=txtmoney.ClientID %>").val(money);
$("#<%=txtusername.ClientID %>").val(usernametwo);
$("#<%=lblfzname.ClientID %>").val(fzname);
$("#<%=txtcomment.ClientID %>").val(comment);
}
});
}
private void ChanceString()
{
string id = Request["id"].ToString();
HttpCookie cookie = Session["adminsession"] as HttpCookie;
string names = cookie["admingongsi"].ToString();
string username = cookie["adminname"].ToString();
string types = cookie["admintypes"].ToString();
SqlConnection con = new SqlConnection(strConn);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Chance where id='"+id+"' and Cname='"+names+"'",con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string idtwo = dr["id"].ToString();
string namestwo = dr["names"].ToString();
string jilv = dr["jilv"].ToString();
string phone = dr["phone"].ToString();
string discovertime = dr["discovertime"].ToString();
string linkname = dr["linkname"].ToString();
string states = dr["states"].ToString();
string qiandantime = dr["qiandantime"].ToString();
string money = dr["money"].ToString();
string usernametwo = dr["username"].ToString();
string fzname = dr["fzname"].ToString();
string zt = "";
string comment = dr["comment"].ToString();
if (states == "../images/ok.gif")
{
zt = "有效";
}
else
{
zt = "无效";
}
con.Close();
Response.Write(string.Format("{\"idtwo\":\"{0}\",\"namestwo\":\"{1}\",\"jilv\":\"{2}\",\"phone\":\"{3}\",\"discovertime\":\"{4}\",\"linkname\":\"{5}\",\"zt\":\"{6}\",\"qiandantime\":\"{7}\",\"money\":\"{8}\",\"usernametwo\":\"{9}\",\"fzname\":\"{10}\",\"comment\":\"{11}\"}",idtwo,namestwo,jilv,phone,discovertime,linkname,zt,qiandantime, money, usernametwo,fzname,comment));
Response.End();
}
}
#12
我调试的时候说是格式不正确
#13
格式问题解决了,,但是还是错误了alert('服务器出错啦') 弹出这个了
#14
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(){
$.ajax({
url:"Handler.ashx",
type:"post",
data:'id=1&name=csdn',
dataType:"json",
success:function(result){
alert(result.ID+"==="+result.Name);
},
error:function(msg){alert(msg)}
})
})
})
</script>
</head>
<body>
<a>获取</a>
</body>
</html>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
if (!string.IsNullOrEmpty(context.Request.Form["id"]) && !string.IsNullOrEmpty(context.Request.Form["name"]))
{
string id = context.Request.Form["id"];
string name = context.Request.Form["name"];
context.Response.Write("{\"ID\":\"" + id + "\",\"Name\":\"" + name + "\"}");
context.Response.End();
}
}
#15
你参照着改 上面测试可以!
#16
Response.Write(string.Format("{\"ID\":\"{0}\",\"Name\":\"{1}\"}",xx,xx));
#1
JavaScriptSerializer
DataContractJsonSerializer
string str = "{\"root\":{\"row\":[{\"ecode\":\"1\",\"emsg\":\"ok\"},{\"ecode\":\"2\",\"emsg\":\"no\"}]}}";
JavaScriptObject obj = (JavaScriptObject)JavaScriptConvert.DeserializeObject(str);
if (obj.Count > 0)
{
JavaScriptObject ob = (JavaScriptObject)obj["root"];
JavaScriptArray array = (JavaScriptArray)ob["row"];
if (array.Count > 0)
{
foreach (JavaScriptObject o in array)
{
Response.Write(o["ecode"] + "===" + o["emsg"] + "<br/>");
}
}
}
DataContractJsonSerializer
string str = "{\"root\":{\"row\":[{\"ecode\":\"1\",\"emsg\":\"ok\"},{\"ecode\":\"2\",\"emsg\":\"no\"}]}}";
JavaScriptObject obj = (JavaScriptObject)JavaScriptConvert.DeserializeObject(str);
if (obj.Count > 0)
{
JavaScriptObject ob = (JavaScriptObject)obj["root"];
JavaScriptArray array = (JavaScriptArray)ob["row"];
if (array.Count > 0)
{
foreach (JavaScriptObject o in array)
{
Response.Write(o["ecode"] + "===" + o["emsg"] + "<br/>");
}
}
}
#2
dataType改为json
Response.Write(string.format("{\"ID\":\"{0}\",\"Name\":\"{1}\"}",xx,xx));
//输出类型改为json
success:function(result){
var id=result.ID;
var name=result.Name;
}
//取值改为这样取
#3
谢谢楼上的回答
那么那个数据库查询那些逻辑写到什么位置呢?怎么写呢?
#4
你原来怎么写就怎么写,只是最后拼接成json格式的,最后Response.Write输出
#5
也就是说我那个ajax.aspx.cs里面的代码不用修改是吗?
#6
Response.Write(string.format("{\"ID\":\"{0}\",\"Name\":\"{1}\"}",xx,xx));
你这个错误了哦,格式不正确,,,怎么写?
#7
format要大写,我是手写的没注意。
#8
Response.Write(string.Format("{\"idtwo\":\"{0}\",\"namestwo\":\"{1}\",\"jilv\":\"{2}\",\"phone\":\"{3}\",\"discovertime\":\"{4}\",\"linkname\":\"{5}\",\"zt\":\"{6}\",\"qiandantime\":\"{7}\",\"money\":\"{8}\",\"usernametwo\":\"{9}\",\"fzname\":\"{10}\",\"comment\":\"{11}\"}",idtwo,namestwo,jilv,phone,discovertime,linkname,zt,qiandantime, money, usernametwo,fzname,comment));
嗯嗯,但是还是提示错误了,说格式问题方便的话加下我QQ806778705
#9
Response.Write(string.Format("{\"idtwo\":\"{0}\",\"namestwo\":\"{1}\",\"jilv\":\"{2}\",\"phone\":\"{3}\",\"discovertime\":\"{4}\",\"linkname\":\"{5}\",\"zt\":\"{6}\",\"qiandantime\":\"{7}\",\"money\":\"{8}\",\"usernametwo\":\"{9}\",\"fzname\":\"{10}\",\"comment\":\"{11}\"}",idtwo,namestwo,jilv,phone,discovertime,linkname,zt,qiandantime, money, usernametwo,fzname,comment));
//提示我格式不正确....怎么回事
#10
你确定是这一句问题?
你把全部代码都帖上来看看!
你把全部代码都帖上来看看!
#11
好的我都贴出来吧。
function Check(number) {
var id = number;
var cid = "";
var rid = "";
var lid = "";
var jid = "";
var cid = "";
$.ajax({
type: "POST",
url: "ajax.aspx",
data: 'id='+id+'&cid='+cid+'&rid='+rid+'&lid='+lid+'&jid='+jid,
dataType: "json",
timeout: 10000,
error: function() { alert('服务器出错啦!'); },
success: function(data) {
var idtwo = data.idtwo;
var namestwo = data.namestwo;
var jilv = data.jilv;
var phone = data.phone;
var discovertime = data.discovertime;
var linkname = data.linkname;
var zt = data.zt;
var qiandantime = data.qiandantime;
var money = data.money;
var usernametwo = data.usernametwo;
var fzname = data.fzname;
var comment = data.comment;
$("#<%=txtid.ClientID %>").val(idtwo);
$("#<%=txtnames.ClientID %>").val(namestwo);
$("#<%=txtjilv.ClientID %>").val(jilv);
$("#<%=txtphone.ClientID %>").val(phone);
$("#<%=txtdiscovertime.ClientID %>").val(discovertime);
$("#<%=txtlinkname.ClientID %>").val(linkname);
$("#<%=txtstates.ClientID %>").val(zt);
$("#<%=txtqiandantime.ClientID %>").val(qiandantime);
$("#<%=txtmoney.ClientID %>").val(money);
$("#<%=txtusername.ClientID %>").val(usernametwo);
$("#<%=lblfzname.ClientID %>").val(fzname);
$("#<%=txtcomment.ClientID %>").val(comment);
}
});
}
private void ChanceString()
{
string id = Request["id"].ToString();
HttpCookie cookie = Session["adminsession"] as HttpCookie;
string names = cookie["admingongsi"].ToString();
string username = cookie["adminname"].ToString();
string types = cookie["admintypes"].ToString();
SqlConnection con = new SqlConnection(strConn);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Chance where id='"+id+"' and Cname='"+names+"'",con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string idtwo = dr["id"].ToString();
string namestwo = dr["names"].ToString();
string jilv = dr["jilv"].ToString();
string phone = dr["phone"].ToString();
string discovertime = dr["discovertime"].ToString();
string linkname = dr["linkname"].ToString();
string states = dr["states"].ToString();
string qiandantime = dr["qiandantime"].ToString();
string money = dr["money"].ToString();
string usernametwo = dr["username"].ToString();
string fzname = dr["fzname"].ToString();
string zt = "";
string comment = dr["comment"].ToString();
if (states == "../images/ok.gif")
{
zt = "有效";
}
else
{
zt = "无效";
}
con.Close();
Response.Write(string.Format("{\"idtwo\":\"{0}\",\"namestwo\":\"{1}\",\"jilv\":\"{2}\",\"phone\":\"{3}\",\"discovertime\":\"{4}\",\"linkname\":\"{5}\",\"zt\":\"{6}\",\"qiandantime\":\"{7}\",\"money\":\"{8}\",\"usernametwo\":\"{9}\",\"fzname\":\"{10}\",\"comment\":\"{11}\"}",idtwo,namestwo,jilv,phone,discovertime,linkname,zt,qiandantime, money, usernametwo,fzname,comment));
Response.End();
}
}
#12
我调试的时候说是格式不正确
#13
格式问题解决了,,但是还是错误了alert('服务器出错啦') 弹出这个了
#14
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(){
$.ajax({
url:"Handler.ashx",
type:"post",
data:'id=1&name=csdn',
dataType:"json",
success:function(result){
alert(result.ID+"==="+result.Name);
},
error:function(msg){alert(msg)}
})
})
})
</script>
</head>
<body>
<a>获取</a>
</body>
</html>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
if (!string.IsNullOrEmpty(context.Request.Form["id"]) && !string.IsNullOrEmpty(context.Request.Form["name"]))
{
string id = context.Request.Form["id"];
string name = context.Request.Form["name"];
context.Response.Write("{\"ID\":\"" + id + "\",\"Name\":\"" + name + "\"}");
context.Response.End();
}
}
#15
你参照着改 上面测试可以!
#16
Response.Write(string.Format("{\"ID\":\"{0}\",\"Name\":\"{1}\"}",xx,xx));