【发布时间】:2016-10-13 07:40:30
【问题描述】:
我正在通过 jQuery AJAX 在 ASP.Net 中创建评论系统,但我遇到了从数据库加载图像的问题。我在数据库中有 3 个表:
- UserRegistration(uid(PK),username....)
- 个人资料(profileID(PK),uid(FK),fulname,userPic...)
- 评论(cmntID(PK),cmntText,uid(FK)....)
以下是jQuery AJAX代码:
function getcomment() {
var postPlace = $('div.q1');
$.ajax({
url: '/WebForm1.aspx/GetComment',
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: 'json',
type: 'POST',
success: function (data) {
var newData = jQuery.parseJSON(data.d);
var trHtml = '';
var loadPost = postPlace;
$.each(newData, function (i, item) {
trHtml += '<div class="q2" style="background-color: red">' +
'<img src="' + item.userPic + '" class="img-circle" width="32px" height="32px" />'+
'<span>' + item.username + '</span>' +
'<p>' + item.cmntText + '</p>' + '</div>';
});
loadPost.html(trHtml);
}
这是循环内部的item.userPic 的问题。 item.username 和 item.cmntText 运行良好,但 item.userPic 不起作用。但是,当我访问 Profile 表的另一个属性(例如 fulname)时,它就可以工作了。我就是无法访问同一张表的userPic。
这是 C# 中的代码:
[WebMethod]
public static string GetComment()
{
string connection = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
SqlConnection con = new SqlConnection(connection);
SqlDataAdapter da = new SqlDataAdapter("select * from userregistration inner join comment on userregistration.uid=comment.uid inner join profile on comment.uid=profile.uid order by cmntID DESC ", con);
DataTable dt = new DataTable();
da.Fill(dt);
return JsonConvert.SerializeObject(dt);
}
这是我得到的结果:
【问题讨论】:
-
我们需要更多细节。 userpic 是存储为字符串路径还是 base64 编码的字符串?你得到的价值是什么?您期望的价值是多少?
-
我认为他变成了一个json作为响应。那么请您检查您的 console.logs 并检查图像路径(在新标签中打开 URL)以检查可用性。
-
@Rory 他的 userPic 存储为字符串路径,所以我只想使用 jquery ajax 显示来自数据库的存储图像
-
console.log(item.userPic)在console登录什么?