简体   繁体   English

从静态Web服务访问数据并将其返回到jquery

[英]Access and return data from restful webservice to jquery

i have this restful web service where i actually return a list<> in c#. 我有这个宁静的Web服务,实际上我在C#中返回一个list <>。 Now i need to access it with jquery and ajax(this part done) what i haven't and i couldn't do is how to traverse the data and print it one by one on the jquery side. 现在,我需要使用jquery和ajax(完成此部分)访问它,而我不能做的是如何遍历数据并在jquery一侧逐一打印。 Any help will be much appreciated. 任何帮助都感激不尽。 UPDATE: having this code on my rest web service. 更新:在我的其余Web服务上有此代码。 Can you give me a sample code for the $.each function? 您能给我$ .each函数的示例代码吗? because i can't seem to make it work. 因为我似乎无法使其工作。 THANK YOU. 谢谢。

       List<Inventory> IService1.GetInventory()
    {
        List<Inventory> list = new List<Inventory>(); using (SqlConnection testconn = new SqlConnection(connection))
        {
            if(testconn.State == ConnectionState.Closed)
            {
                testconn.Open();
            }

            using(SqlCommand testcmd = new SqlCommand("select * from inventoryitem",testconn))
            {
                SqlDataReader reader = testcmd.ExecuteReader();
                while( reader.Read())
                {
                    Inventory testObj = new Inventory();
                    testObj.InventoryName = reader["StandardCost"].ToString();
                    list.Add(testObj);
                }
            }
        }
        return list;
    }

You should be able to Serialize it as JSON: 您应该能够将其序列化为JSON:

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx http://msdn.microsoft.com/zh-CN/library/system.web.script.serialization.javascriptserializer.aspx

Then iterate over it with an $.each() in jQuery. 然后在jQuery中使用$ .each()对其进行迭代。

http://api.jquery.com/jQuery.each/ http://api.jquery.com/jQuery.each/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM