Handler1.ashx文件,前台用AJAX怎么取数据
本帖最后由 chirea 于 2012-11-27 17:37:05 编辑 Handler1.ashx文件代码
/// <summary>
/// ProductHandler 的摘要说明
/// </summary>
public class ProductHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/xml";
string PCid = context.Request["ID"];
DataSet ProClass = Business.BLL_Product.getProductClassByID(PCid);
// ProClass.WriteXml(context.Response.Output);
context.Response.Write(ProClass.GetXml());
}
public bool IsReusable
{
get
{
return false;
}
}
}
<script type="text/ecmascript">
$(function () {
$('a').click(function () {
$('.children').css('display', 'none');
$(this).next('.children').css('display', 'block');
var Pid = $(this).attr('id');
$.post("ProductHandler.ashx", { "ID": Pid }, function (xhr) {
var items = xhr.firstChild;
items = $(items);
var str = "";
for (var i = 0; i < items[0].childNodes.length; i++) {
var ID = $(items[0].childNodes[i]).find("ID").text();
var C4_Picture1 = $(items[0].childNodes[i]).find("C4_Picture1").text();
var ProductID = $(items[0].childNodes[i]).find("ProductID").text();
str += '<div class="ProImg"><div> <a href="ProductDetail.aspx?id=' + ID + '"><img src="' + C4_Picture1 + '" alt="Handler1.ashx文件,前台用AJAX如何取数据" /></a></div>' + '<div class="ProductName"> ' + ProductID + '</div> </div>'
}
$("producc").html(str);
});
});
})
$.post("getdata.ahsx",{"type":$("#txttype").val()},function(data,status){
if(status!="success"){
return;
}
var jsonData = $.ParseJSON(data);
for(var i=0;i<jsonData.length;i++){
//你的操作
}
})
var request = new Sys.Net.WebRequest();
request.set_url("ProductHandler.ashx");
request.set_httpVerb("POST")
request.add_completed(
function (response) {
if (response.get_responseAvailable()) {
var prol = response.get_object();
alert(prol.ID);
}
});
var requestBody = String.format("ID={0}", Pid);
request.set_body(requestBody);
request.invoke();
public class ProductList {
public string ID { get; set; }
public string ProductID { get; set; }
public string ProductImages { get; set; }
}
[{"ID":"49613990-edb5-406e-82d4-4d80898b7593","ProductID":"BBB","ProductImages":""},{"ID":"1b2d938c-5ab2-4865-951e-4e5c50d0f4de","ProductID":"a2","ProductImages":""},{"ID":"7d6b1884-7112-4be1-9a0f-5a78399f3124","ProductID":"A002","ProductImages":""},{"ID":"8e31568d-2ee6-4d92-9930-64dddbd82bd7","ProductID":"A003","ProductImages":""},{"ID":"bf35db9f-8476-4709-9921-7b938334d439","ProductID":"ss","ProductImages":""}]
request.add_completed(
function (response) {
//这里不知道怎么取.
没有取过JSON数据.能举例说一下吗?
});