前台代码:
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
function showAll() {
$.get("Default2.aspx?select=yes", function (data) {
var json = eval(data);
var html = "<table border=1 bordercolor=red cellspacing=1>";
html += "<tr bgcolor='yellow'><td>客户ID</td><td>客户类型</td><td>用户ID</td></tr>";
for (var i = 0; i < json.length; i++) { html += "<tr>";
html += "<td>" + json[i].Comp_companyid + "</td><td>" + json[i].Comp_type + "</td><td>" +json[i].User_userid + "</td>";
html += "</tr>";
}
html += "</table>";
$("#nihao").html(html);
});
}
</script>
后台:
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (Request.QueryString["select"] != null)
{
List<Comany> list = ComanyBLL.GetAllComany();
string str = Newtonsoft.Json.JsonConvert.SerializeObject(list);
Response.Write(str);
Response.End();
}
}
User_userid 是个javascript object
你直接访问Default2.aspx?select=yes 看看返回值呢
是有返回值的,请指教,为什么会显示[objct Ojbect]我用的是连接两张表连接查询
public Comany(int comp_companyid, string comp_type, int user_userid) { this.comp_companyid = comp_companyid; this.comp_type = comp_type; this.user_userid =new Users(user_userid); }
@晴天小: json[i].User_userid 改成 json[i].User_userid.User_userid
谢谢你,我的问题已经解决,非常感谢
Comany的定义是什么样的?
public Comany(int comp_companyid, string comp_type, int user_userid)
{
this.comp_companyid = comp_companyid;
this.comp_type = comp_type;
this.user_userid =new Users(user_userid);
}
@晴天小: 你这个user_userid里存的本身就是一个对像,当然解析的json里就是一个object
那怎么写,我是两张表的连接查询
private const string SELECT_Model = "select comp_companyid,comp_type,user_userid from Company left join users on Comp_PrimaryUserId=User_UserId where Comp_Type is not null and user_userid is not null";
public List<Comany> GetALLTypes()
{
List<Comany> types = new List<Comany>();
SqlDataReader dr = null;
try
{
dr = SqlHepler.ExecuteReader(SELECT_Model, null);
while (dr.Read())
{
Comany t = new Comany(dr.GetInt32(0), dr.GetString(1),dr.GetInt32(2)); types.Add(t);
}
}
catch (SqlException e)
{
throw e;
}
finally
{
if (dr != null && !dr.IsClosed)
{
dr.Close();
dr = null;
}
}
return types;
}
}
this.user_userid =new Users(user_userid);改成
this.user_userid =user_userid;
我是两张表的连接查询呀,user_userid是Users表里面的ID
@晴天小: json[i].User_userid.User_userid 像上面說的。這樣不行嗎?