[英]How to access data returned from Ajax post method call with index just like an array
Here is my code and it is not working to success function. 这是我的代码,它对成功功能无效。 Is there a possibility that I am not specifying in the right way tr and td elements
我是否有可能没有以正确的方式指定tr和td元素
$(document).ready(function () {
$("#search").blur(function () {
//alert("ilir");
$.ajax({
type: "POST",
data: { "barkodi": $(this).val() },
dataType: 'json',
success: function (data) {
$('tr[class!="info"]').remove().after(function () {
$.each(function (index, data) {
$("tr").append(
$("<tr></tr>").html(
$("<td></td>").text(data[0].Barkodi),
$("<td></td>").text(data[0].Emri),
$("<td></td>").text(data[0].Cmimi),
$("<td></td>").text(data[0].Sasia))
);
});
});
}
});
});
})
And here is my controller and action method I dont where is my bug here :( 这是我的控制器和操作方法,我不知道我的错误在这里:(
[HttpPost]
[AllowAnonymous]
public JsonResult Index(string barkodi)
{
var produkti = _context.Produktets.SingleOrDefault(x => x.Barkodi == barkodi);
List<ProduktetCustom> prod = new List<ProduktetCustom>();
ProduktetCustom produkt = new ProduktetCustom()
{
Barkodi = produkti.Barkodi,
Emri = produkti.Emri,
Sasia = produkti.Sasia,
Cmimi = produkti.Cmimi,
Pershkrimi = produkti.Pershkrimi,
IsDeleted = produkti.IsDeleted,
DataModifikimit = produkti.DataModifikimit,
DataShtimit = produkti.DataShtimit
};
prod.Add(produkt);
return Json(prod,JsonRequestBehavior.AllowGet);
}
You should use forEach
function defined in Array
instead of $.each
. 您应该使用在
Array
定义的forEach
函数而不是$.each
。
Your code will look like this: 您的代码将如下所示:
success: function (data) {
$('tr[class!="info"]').remove().after(function () {
data.forEach(function (item, index) {
$("tr").append(
$("<tr></tr>").html(
$("<td></td>").text(item.Barkodi),
$("<td></td>").text(item.Emri),
$("<td></td>").text(item.Cmimi),
$("<td></td>").text(item.Sasia)
)
);
});
});
}
You're correct; 没错 the issue is how you're adding the elements in the
html()
method. 问题是如何在
html()
方法中添加元素。 It accepts only a string or a function. 它仅接受字符串或函数。 You're also looping through the
data
array, but repeatedly adding only the content from the first item. 您还将遍历
data
数组,但重复地仅添加第一项的内容。 You're also attempting to place content after()
an element which has been removed, which won't work. 您还试图将内容放置
after()
已删除的元素after()
该元素将不起作用。
Try this: 尝试这个:
success: function (data) {
var html = data.map(function(o) {
return `<tr><td>${o.Barkodi}</td><td>${o.Emri}</td><td>${o.Cmimi}</td><td>${o.Sasia}</td></tr>`;
}).join('');
$('table tbody').append(html);
}
var data = [{ Barkodi: 'Barkodi 1', Emri: 'Emri 1', Cmimi: 'Cmimi 1', Sasia: 'Cmimi 1', }, { Barkodi: 'Barkodi 2', Emri: 'Emri 2', Cmimi: 'Cmimi 2', Sasia: 'Cmimi 2', }] var html = data.map(function(o) { return `<tr><td>${o.Barkodi}</td><td>${o.Emri}</td><td>${o.Cmimi}</td><td>${o.Sasia}</td></tr>`; }).join(''); console.log(html); $('table tbody').append(html);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <table><tbody></tbody></table>
Not tested but you can try parseJSON
. 未经测试,但您可以尝试
parseJSON
。
var objArray = $.parseJSON(data);
$.each(function (index, objArray) {
//other stuff
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.