【问题标题】:why isn't jscript displaying my items?为什么 jscript 不显示我的项目?
【发布时间】:2013-05-16 16:39:41
【问题描述】:

我有以下 Javascript:

$(document).ready(function () {
        $.getJSON("api/products/",
        function (data) {
            $.each(data, function (key, val) {

                // Format the text to display.
                var str = val.Name + ': $' + val.Price;

                // Add a list item for the product.
                $('<li/>', { text: str })    
                .appendTo($('#products'));   
            });
        });
    });

我的问题是产品列表没有显示。我有另一个功能可以搜索列表中的单个项目并且能够显示这些项目,但为什么这不起作用?

这是存储信息的类:

public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }

这是控制器:

public class ProductsController : ApiController
    {
        Product[] products = new Product[] 
        { 
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
            new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
            new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
        };
        public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }

        public Product GetProductById(int id)
        {
            var product = products.FirstOrDefault((p) => p.Id == id);
            if (product == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return product;
        }

        public IEnumerable<Product> GetProductsByCategory(string category)
        {
            return products.Where(
                (p) => string.Equals(p.Category, category,
                    StringComparison.OrdinalIgnoreCase));
        }
    }

我是 ASP.NET 的初学者,所以请告诉我我做错了什么。

【问题讨论】:

  • 您是否尝试过使用诸如 firebug 之类的工具来确保您实际上是从服务器获取数据?
  • 产品是真实的ID吗?你能验证循环正在运行吗?返回的数据是否正确?
  • 我验证了循环没有运行,但我在它下面有一个函数,可以通过它们的 ID 调用各个产品,并且似乎工作正常。

标签: javascript asp.net-mvc-4


【解决方案1】:

我认为您的意思是使用文本作为创建

  • 节点的初始 jQuery 选择器的方法。

    将第二个参数传递给 $() 实际上会产生一个“上下文”,它将尝试限制选择器从哪里开始。见:http://api.jquery.com/jQuery/#jQuery1/

    http://jsfiddle.net/A4tk9/

    $(function() {
    
        var data = [{ Name: 'test1', Price: 1000}, { Name: 'test2', Price: 25}];
    
        $.each(data, function (key, val) {
            // Format the text to display.
            var str = val.Name + ': $' + val.Price;
    
            // Add a list item for the product.
            $('<li/>')
            .text(str)
            .appendTo($('#products'));   
        });
    
    });
    
  • 【讨论】:

    • 抱歉,我对此进行了测试,但也无法正常工作。
    • 您查看过 JSFiddle 吗?它在那里工作。问题可能是您的数据返回为空,或者不是您期望的格式?
    • 根据this的说法,我的浏览器可能不支持jquery/1.7.1...我用的是chrome,这样不行吗?
    • 我目前的演出使用 1.7.2,我在 Chrome 中进行了所有初始开发(然后切换到 FF 和 IE 进行测试)。你在运行什么操作系统?什么版本的 Chrome?
    猜你喜欢
    • 2018-12-07
    • 2012-02-04
    • 1970-01-01
    • 2016-10-27
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多