最近做一个东西,要在aspnet里面的TextBox上加一个中文自动匹配。相关微软或者其他ASPNET提供的插件没用过,因为之前用的是JAVA,jquery用的比较多,于是就选用了jquery中ajax的传递方式,然后ASPNET中采用了ashx一般处理程序。ashx也是第一次使用。jquery的操作时根据之前从别人拿来的程序,如下:ajax部分
$.ajax({
type: "POST",
//url:"jsondata1.ashx?type="+escape(ghdw_Value)+"", //(2)
url:"jsondata1.ashx?type="+ghdw_Value+"", //(1)
async:true,
data: obj, //(3)
dataType: "json", //(4)
success: function(data){
// alert("succ");
// alert(data.length);
//alert(data[0]["id"]);
var aData = [];
for(var i=0;i<data.length;i++)
{
if(data[i]["name"]!="")
{
aData.push(data[i]["name"]);
}
}
//将返回的数据传递给实现搜索输入框的输入提示js类
searchSuggest.dataDisplay(aData);
}
});
后台ashx中:
using System;
using System.Web;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Newtonsoft.Json;//用到newtonsoft.json.dll ,对应.net2.0版本的
using Newtonsoft.Json.Linq;
public class jsondata1 : IHttpHandler {
Class1 mc = new Class1();
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";//(5)
string type = "";
type = context.Request["type"];//
// type = context.Server.UrlDecode(type);//(6)
string sql = string.Format("select distinct name from 1_table where name like '%{0}%'",type.Trim());
DataSet ds = new DataSet();
mc.fillData(ds,"namedata",sql);
List<Dictionary<String, String>> listname = new List<Dictionary<string, string>>();
if(ds.Tables["namedata"].Rows.Count>0)
{
foreach(DataRow dr in ds.Tables["namedata"].Rows)
{
Dictionary<String, String> dic = new Dictionary<string, string>();
dic.Add("name", dr["chanwuname"].ToString().Trim());
listname.Add(dic);
}
}
string json1 = JsonConvert.SerializeObject(listname,Formatting.Indented);
context.Response.Write(json1);
}
public bool IsReusable {
get {
return false;
}
}
}
刚开始在360,火狐浏览器都正常,但是就是IE中不好使。以为是jquery版本的问题,1.4.3,可以把ajax部分替换成别得东西都是正常的。最后经过琢磨是ajax向后台发送时设置的参数问题。前台发送一个字符串,后台接受后,将需要的数据通过包装成json格式返回。因此在ajax注释(3)的地方, data: obj, //(3),这里对于IE应该注释掉,对于其他浏览器也没有关系,猜想可能是下面的dataType:'json',设置后这里的data就可以不设,但是不能为obj,否则接受数据格式不对。
$.ajax({
.....
async:true,
// data: obj, //(3)
dataType: "json", //(4)
....
});
这样果然,在IE中也可以自动进行BoxText的数据匹配了,但是又发现一个问题,就是英文和数字的都可以,但是中文的,返回没有数据。猜想可能是中文ajax传输时,以前用jsp时网页传输中文都会进行编码,那么这里是不是也需要啊。于是又进行修改如下:用escape()
$.ajax({
type: "POST",
url:"jsondata1.ashx?type="+escape(ghdw_Value)+"", //(2)...................note
// url:"jsondata1.ashx?type="+ghdw_Value+"", //(1)
async:true,
// data: obj, //(3)
dataType: "json", //(4)
.....
然后在ashx中也加入一句解码的:
public class jsondata1 : IHttpHandler {
Class1 mc = new Class1();
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";//(5)
string type = "";
type = context.Request["type"];//
type = context.Server.UrlDecode(type);//(6)加上这句
..
下面就OK 了。
想想这个问题,纠结了2天了,终于解决了。但是解决过程中遇到好多问题需要总结一下。
1遇到问题的,分析出问题出在哪里;总是转圈,却发现不了问题,通过测试将可能问题点一一排除,刚才是以为是jquery的版本问题,但是通过其他一两个的功能可以看出只要是不涉及到ajax的,我尝试的都没有问题,于是可以定位到ajax上。但是火狐,360都没有问题,但是IE为什么会有问题那?网上狂搜一下,最后感觉是要么没有将数据传给后台,要么是后台没有返回数据或者是没有返回正确格式的数据。格式问题,将data,dataType,按照可能的情况进行了尝试,结果只要将data注释掉,IE就可以正常返回请求的英文数据,请求参数为中文却不成功,心想可能和汉字编码有关,于是在前台对汉字编码,后台再解码,成功。遗留的问题是对于IE8,设置上data:obj,为什么不行?
2.分析问题的思路和解决的方式有点拖拉,有些时候可能猜到了问题所在,却没有实践,导致问题一直没有解决。