简体   繁体   English

如何像数组一样使用索引访问从Ajax post方法调用返回的数据

[英]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.

相关问题 如何对从 AJAX 调用返回的数据进行分组? - How do I group like data returned from an AJAX call? 在我的情况下,如何在 AJAX 回调 function 之外访问从 ajax 调用返回的数据? - How can I access data returned from ajax call outside AJAX callback function in my case? 如何从.POST ajax调用中获取返回对象的类型? - How to get the type of the returned object from .POST ajax call? 如何使用普通 JavaScript 访问 AJAX 调用中返回的数据? - How do I access the data returned in an AJAX call with vanilla JavaScript? 如何访问从 MVC 视图中的 ajax 调用调用的控制器返回的数据 - How do I access data returned from a controller invoked by an ajax call in an MVC View 从 RxJs Observable 方法调用访问我返回的数据 - Access my returned data from an RxJs Observable method call 如何在没有参数的情况下调用c#方法并访问返回的数据? - How to call c# method with no parameters and access returned data? 如何从jQuery中从Ajax调用返回的数据中获取对象? - How to get object from data returned from Ajax call in jQuery? ajax调用结束后如何从ajax调用访问数据 - How to access data from ajax call after the ajax call ends 如何在模态框中显示从ajax调用返回的数据? - How to show data returned from ajax call in a modal box?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM