过滤AJAX请求调用

时间:2016-11-18 12:59:36

标签: jquery ajax

我遇到了与过滤的AJAX调用相关的问题,我正在使用ZipCodes和Regions填充列表。

我从ZipCodeService.ashx获取的JSON数组包含字段" Name"和#34;代码"我试图只得到具有" SampleCity"在“名称”字段中。但是,现在所有的字段及其数据都被返回,因此过滤器显然不能正常工作或者像我期望的那样。非常感谢任何帮助!

我是JQuery / Javascript的新手,也是AJAX的新手,所以请耐心等待。

site.js

$.ajax({
    async: true,
    contentType: 'application/json; charset=utf-8',
    method: "POST",
    url: "../ZipCodeService.ashx",
    data: { Name: "SampleCity" },
    success: function (data) {

            var $select = $('#list1');
            $.each(data, function (i, item) {
                $('<option>',
                {
                    value: item.Code + " - " + item.Name,
                }).html(item.Code + " - " + item.Name).appendTo($select),
                '</option>';
            });

    }
});

ZipCodeService.ashx

public class ZipCodeService : IHttpHandler
{
    [DataContract]
    public class ZipCode
    {
        [DataMember]
        public string Code { get; set; }

        [DataMember]
        public string Name { get; set; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";

        var p = new ZipCode[] {
            new ZipCode()
        {
            Code = "00001",
            Name = "SampleCity"
        },new ZipCode()
        {
            Code = "00002",
            Name = "SampleCity2"
        },new ZipCode()
        {
            Code = "00003",
            Name = "SampleCity3"
        },new ZipCode()
        {
            Code = "00004",
            Name = "SampleCity4"
        }
        };

        MemoryStream stream1 = new MemoryStream();
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ZipCode[]));
        ser.WriteObject(stream1, p);

        context.Response.Write(Encoding.UTF8.GetString(stream1.ToArray()));
    }

}

2 个答案:

答案 0 :(得分:1)

我认为您可以使用if条件:

$.ajax({
    async: true,
    contentType: 'application/json; charset=utf-8',
    method: "POST",
    url: "../ZipCodeService.ashx",
    data: {
        Name: "SampleCity"
    },
    success: function(data) {
        var $select = $('#list1');
        $.each(data, function(i, item) {
            if (Item.Name == "SampleCity") {
                $select.append('<option value="' + item.Code + '">' + item.Name + '</option>');
            }
        });
    }
});

答案 1 :(得分:0)

如果您想在客户端进行过滤,可以尝试Surjeet的解决方案。

但是如果您需要在服务器端执行与已发送输入数据相同的操作,则可以尝试以下操作。

我已使用JavaScriptSerializer序列化传入的数据,您可以为此System.Web.Script.Serialization添加名称空间,并引用程序集System.Web.Extensions.dll

public class ZipCodeService : IHttpHandler
{
    [DataContract]
    public class ZipCode
    {
        [DataMember]
        public string Code { get; set; }

        [DataMember]
        public string Name { get; set; }
    }

    [Serializable]
    public class Zip
    {
       public string Name { get; set; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";

        var p = new ZipCode[] {
            new ZipCode()
        {
            Code = "00001",
            Name = "SampleCity"
        },new ZipCode()
        {
            Code = "00002",
            Name = "SampleCity2"
        },new ZipCode()
        {
            Code = "00003",
            Name = "SampleCity3"
        },new ZipCode()
        {
            Code = "00004",
            Name = "SampleCity4"
        }
        };

        var sr = new StreamReader(context.Request.InputStream);
        var stream = sr.ReadToEnd();    
        var serializer = new JavaScriptSerializer();
        var postedData = serializer.Deserialize<Zip>(stream); 

        var filtered = p.Where(z => z.Name == postedData.Name).ToArray(); 

        MemoryStream stream1 = new MemoryStream();
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ZipCode[]));
        ser.WriteObject(stream1, filtered);

        context.Response.Write(Encoding.UTF8.GetString(stream1.ToArray()));
    }

}
相关问题