2016-11-18 63 views
0

我有相關的過濾AJAX調用我提出來用拉鍊碼和地區名單的問題。過濾的AJAX請求調用

我從ZipCodeService.ashx中收集的JSON數組包含字段「名稱」和「代碼」,我試圖只在名稱字段中獲得具有「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

我在此代碼中看不到「過濾器」。你只是附加的所有結果以'#list1' – Turnip

+0

我,毫無疑問是一個初學者AJAX和JQuery所以我親自期待數據:{}屬性是過濾器。我想這不是這種情況? @Turnip – Xariez

+1

你可以添加ZipCodeService.ashx的代碼嗎?您正在將JSON對象作爲參數發送到頁面。如果.ashx頁面期望它一切都應該正常工作,如果不是這樣,也許你應該發送一些不同的東西。 – Bardo

回答

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>'); 
      } 
     }); 
    } 
}); 
+0

剛剛得到它固定我自己這樣!我將這個標記爲答案,因爲它確實是修正它的代碼。 – Xariez

+0

@Xariez你不希望過濾器發生在服務器端而不是客戶端嗎? – Aruna

0

如果你婉t在客戶端進行過濾,您可以嘗試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())); 
    } 

}