2012-09-29 19 views
0

我是Ajax和javqscript的新手。如何在表格中顯示圖像返回Json數據的路徑

我返回我的列表與圖像路徑通過Json。任何人都可以告訴我顯示圖像的方式嗎?

我正在使用Ajax調用加載數據。

Ajax Call 
var TableHeaderArray = ["Person", "Details"]; 
$.ajax({ 
url: '/Home/Persondetails', 
type: 'POST', 
dataType: "json", 
beforeSend: function() { 
}, 
success: function (response) { 
$('#Person').append(CreateHTMLTableFromJSON(response, "lightPro", TableHeaderArray)).fadeIn(); 
}, 
error: function (error) { 
alert(error); 
} 
}); 

控制器

[HttpPost] 
public ActionResult Persondetails() 
{ 
SqlConnection con = new SqlConnection(); 
con.ConnectionString = ConfigurationManager.ConnectionStrings["PersonConnectionString"].ConnectionString; 
con.Open(); 
SqlCommand cmd = new SqlCommand("GetDetails", con); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.ExecuteScalar(); 
DataSet ds = new DataSet(); 
SqlDataAdapter sda = new SqlDataAdapter(cmd); 
sda.Fill(ds); 
List<Person> Details = new List<Person>(); 
foreach (DataRow dr in ds.Tables[0].Rows) 
{ 
Details.Add(new Person() 
{ 
Id = dr["Id"].ToString(), 
Name = dr["Name"].ToString(), 
Age = dr["Age"].ToString(), 
Job = dr["Job"].ToString(), 
images = dr["Images"].ToString(), 
}); 
} 
return Json(Details); 
} 
+0

如果你能告訴我們你正在使用它會幫助我們看到了問題,可能是什麼的代碼。 – Lix

+1

顯示您從ajax調用中獲得的json格式 – Hearaman

回答

0

對於axample這種方式:

$.ajax({ 
     url: 'some_url', 
     success: function(data) { 
      // Here will be image inserting 
     } 
    }); 

現在我們有兩種情況:

1)本圖像(例如ID = imagId)是已經在您的網頁上。在這種情況下,success回調,你應該做到以下幾點:

$('#imgId').attr('src', data.imageUrl); 

2)你必須頁面上插入新的形象。所以,在這種情況下success回調將是這樣的:

var img = $('<img/>', {'src': data.imageUrl}); 
$('#whereToInsertId').append(img); 

它應該工作;)

相關問題