我有相关的过滤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()));
}
}
我在此代码中看不到“过滤器”。你只是附加的所有结果以'#list1' – Turnip
我,毫无疑问是一个初学者AJAX和JQuery所以我亲自期待数据:{}属性是过滤器。我想这不是这种情况? @Turnip – Xariez
你可以添加ZipCodeService.ashx的代码吗?您正在将JSON对象作为参数发送到页面。如果.ashx页面期望它一切都应该正常工作,如果不是这样,也许你应该发送一些不同的东西。 – Bardo