要写一个WebService,说完需求后感觉是要通过输入的值获取对应信息,所以就想创建一个含有键值对的list通过ajax来获取值,但不知道该怎么写求大佬们指点
下面是代码残局using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;namespace WebApplication1
{
    /// <summary>
    /// test 的摘要说明
    /// </summary>
    public class test : IHttpHandler
    {
        
        [WebMethod]
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string number1 = context.Request["aaa"];
            //var result = Int32.Parse(number1) + Int32.Parse(number2);
            List<string> strList = new List<string>();
            strList.Add("qwer");
            strList.Add("w3c");
            if (context.Request.QueryString["number1"] == " 53014")
                context.Response.Write("result is" + " " + strList);
        }
       
       // [WebMethod(Description = "list")]
        //public void Kh(HttpContext context, string a)
        //{
        //    context.Response.ContentType = "text/plain";
        //    string number1 = context.Request["aaa"];        //    List<string> strList = new List<string>();
        //    strList.Add("商品名称,商品颜色");
        //    //if (number1 == "53014")
        //    //    context.Response.Write(strList);
          
        //    //return strList;
        //}
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoginGet.aspx.cs" Inherits="WebApplication1.LoginGet" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>测试页面</title>
     <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
   <script>
     function callAjax() {
         $.ajax({
             type: "POST", //提交方式 
             url: "/test.ashx",//路径 
             data: {
                 "aaa": $("#number1").val(),
                 
             },
             success: function (result) {//返回数据根据结果进行相应的处理 
                 alert(result);
                
             }
         }); 
     }
 </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
           <label>请输入编号</label>  <input type="number" name="number1" id="number1" />
                <input type="button" value="Add" onclick="callAjax();" />
           
        </div>
    </form>
</body>
</html>

解决方案 »

  1.   

    直接把你的List<T> 序列化成字符串丢给前台就好了。
    使用 json.net库
      

  2.   

    你要的是Dictionary,根据key取的对应的value,不过数据量不大,也不会数据库的话,可以直接整体json返回前端,前端进行遍历读取
      

  3.   

     string JsonStr = JsonConvert.SerializeObject(List);
      

  4.   

    额 现在是用dictionary来做个假数据,通过在.aspx的input框输入键值,然后点击按钮获得对应的值。就是做一个这样简单的WebService自己差的知识太多  又不知道从哪学起.....