我正在使用JQgrid来显示数据库记录。现在我需要将请求传递给处理程序文件以使用jquery ajax调用来检索数据。但是除了jason数据字段之外所有其他数据字段都会出现。 下面我发布了我的.handler和.aspx代码..
来自服务器代码的.handler文件..
json ="";
json = json + "{\n";
json = json + " \"page\":\""+intpage+"\",\n";
json = json + "\"total\":"+total_pages+",\n";
json = json + "\"records\":"+total+",\n";
json = json + "\"rows\": [";
rc =false;
while(rs.Read()){
if(rc){
json = json + ",";
}
json = json + "\n{";
json = json + "\"price\":\"" + Convert.ToInt32(rs["price"]) + "\",";
json = json + "\"cell\":[" + Convert.ToInt32(rs["price"]) + "";
json = json + ",\"" + Convert.ToString(rs["username"]) + "\"";
json = json + ",\"" + Convert.ToString(rs["ordinal"]) + "\"";
json = json + ",\"" + Convert.ToString(rs["authcode"]) + "\"";
json = json + ",\"" + Convert.ToString(rs["extension"]) + "\"";
json = json + ",\"" + Convert.ToString(rs["trunk"]) + "\"";
json = json + ",\"" + Convert.ToString(rs["dialnumber"]) + "\"";
json = json + ",\"" + Convert.ToString(rs["dialdate"]) + "\"";
json = json + ",\"" + Convert.ToString(rs["dialtime"]) + "\"";
json = json + ",\"" + Convert.ToString(rs["duration"]) + "\"";
json = json + ",\"" + Convert.ToString(rs["destination"]) + "\"";
json = json + ",\"" + Convert.ToString(rs["price"]) + "\"";
json = json + ",\"" + Convert.ToString(rs["toc"]) + "\"]";
json = json + "}";
rc=true;
}
json = json +"]\n";
json = json +"}";
HttpContext.Current.Response.Write(json);
这是我的.aspx代码..
var jason;
$(document).ready(function() {
{
var URL='getGriddahico.ashx';
$.ajax({
url:URL,
type:'GET',
//datatype:'jason',
success: function (data) {
jason = data;
// alert(jason);
}
});
}
});
但是在警告框中我正在关注数据字段......
{
"page":"-2147483648",
"total":-2147483648,
"records":150508,
"rows":[]
}
答案 0 :(得分:0)
有一些拼写错误,你可以试试这个:
$(document).ready(function() {
var URL='getGriddahico.ashx';
$.ajax({
url:URL,
type:'GET',
datatype:'json',
success: function (data) {
$.each(data, function(i, jason){
console.log(jason.price); // <--should print your price
});
}
});
});
答案 1 :(得分:0)
Vikas,创建一个存储数据读取器数据的对象,如
List<cutomobject> result = new List<customobject>();
while(rs.Read()){
result.add(new customobject{ price= "Convert.ToInt32(rs["price"])", cell = "Convert.ToInt32(rs["cell"])" ,... });
}
var jsonData = new
{
total = result.Count() / 15,
page = 1,
records = result.Count(),
rows = result
};
HttpContext.Current.Response.Write(json);
希望这能解决您的问题。
答案 2 :(得分:0)
你可以这样
$(document).ready(function() {
{
var URL='getGriddahico.ashx';
$.ajax({
url:URL,
type:'GET',
datatype:'jason',
success: function (data) {
for (var i = 0; i < data.length; i++)
{
data[i].price;
data[i].cell;
}
}
});
}
});
您也可以浏览此链接
convert from SqlDataReader to JSON
http://www.west-wind.com/weblog/posts/2009/Apr/24/JSON-Serialization-of-a-DataReader
我建议您使用jtamplate和json
http://blog.jambura.com/2011/12/18/jquery-json-and-jtemplates-for-ajax-driven-web-app/