Javascript 为什么不是';显示我的项目的jscript?

Javascript 为什么不是';显示我的项目的jscript?,javascript,asp.net-mvc-4,Javascript,Asp.net Mvc 4,我有以下Javascript: $(document).ready(function () { $.getJSON("api/products/", function (data) { $.each(data, function (key, val) { // Format the text to display. var str = val.Name + ': $' + v

我有以下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 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));
        }
    }
公共类产品控制器:ApiController
{
产品[]产品=新产品[]
{ 
新产品{Id=1,Name=“番茄汤”,Category=“杂货”,Price=1},
新产品{Id=2,Name=“Yo”,Category=“Toys”,Price=375万},
新产品{Id=3,Name=“Hammer”,Category=“Hardware”,Price=16.99M}
};
公共IEnumerable GetAllProducts()
{
退货产品;
}
公共产品GetProductById(int id)
{
var product=products.FirstOrDefault((p)=>p.Id==Id);
如果(产品==null)
{
抛出新的HttpResponseException(HttpStatusCode.NotFound);
}
退货产品;
}
公共IEnumerable GetProductsByCategory(字符串类别)
{
退货。在哪里(
(p) =>string.Equals(p.Category,Category,
StringComparison(Ordinalingorecase));
}
}

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

我想您是想使用文本作为创建
  • 节点的初始jQuery选择器的方法

    将第二个参数传递给$()实际上会产生一个“上下文”,它将尝试限制选择器的起始位置。见:

    $(函数(){
    var data=[{Name:'test1',Price:1000},{Name:'test2',Price:25}];
    $。每个(数据、函数(键、值){
    //格式化要显示的文本。
    var str=val.Name+':$'+val.Price;
    //为产品添加列表项。
    $(“
  • ”) .文本(str) 。附件($(“#产品”); }); });
  • 我想您是想使用文本作为创建
  • 节点的初始jQuery选择器的方法

    将第二个参数传递给$()实际上会产生一个“上下文”,它将尝试限制选择器的起始位置。见:

    $(函数(){
    var data=[{Name:'test1',Price:1000},{Name:'test2',Price:25}];
    $。每个(数据、函数(键、值){
    //格式化要显示的文本。
    var str=val.Name+':$'+val.Price;
    //为产品添加列表项。
    $(“
  • ”) .文本(str) 。附件($(“#产品”); }); });
  • 您是否尝试过使用firebug之类的工具来确保您确实从服务器获取了数据?产品是真实的id吗?你能确认循环正在运行吗?返回的数据正确吗?我验证了循环没有运行,但我在它下面有一个函数,可以根据产品的ID调用各个产品,而且似乎工作正常。您是否尝试过使用firebug之类的工具来确保您确实从服务器获取了数据?产品是真实的ID吗?你能确认循环正在运行吗?返回的数据正确吗?我验证了循环没有运行,但我在它下面有一个函数,它可以根据各个产品的ID调用各个产品,并且似乎工作正常。抱歉,我测试了这个,但我也无法让它工作。您查看了JSFIDLE吗?它在那里工作。问题可能是您的数据返回为空,或者不是您期望的格式?根据,我的浏览器可能不支持jquery/1.7.1。。。我用的是chrome,不是很好吗?我现在的工作是使用1.7.2,我所有的初始开发都是用chrome完成的(然后切换到FF&IE进行测试)。你在运行什么操作系统?什么版本的Chrome?抱歉,我测试了这个,但我也无法让它工作。你查看了JSFIDLE吗?它在那里工作。问题可能是您的数据返回为空,或者不是您期望的格式?根据,我的浏览器可能不支持jquery/1.7.1。。。我用的是chrome,不是很好吗?我现在的工作是使用1.7.2,我所有的初始开发都是用chrome完成的(然后切换到FF&IE进行测试)。你在运行什么操作系统?什么版本的Chrome?
    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));
            }
        }
    
    $(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'));   
        });
    
    });