在html页面的第一种写法
<script> $.Ajax({ type: 'post', url: 'http://www.whjtwaap.com/ashx/test.ashx', dataType: 'JSON', beforeSend: function () { $("#loadimg").show(); }, success: function (data) { var html = '<ul class="mui-table-view">'; for (var i = 0; i < data.length; i++) { html += '<li class="mui-table-view-cell mui-media"><a href="javascript:;">'; html += '<img class="mui-media-object mui-pull-right" src="images/yuantiao.jpg">'; html += '<div class="mui-media-body">'; html += 'test'; html += '<p class="mui-ellipsis">' + data[i]["Title"] + '</p>'; html += '</div>'; html += '</a></li>'; } html += '</ul>'; $('#divCustomers').html(html); }, complete: function () { $("#loadimg").hide(); } })
在html页面的第二种写法//$.getJSON("http://www.whjtwaap.com/ashx/test.ashx", function(data) {// var html = '<ul class="mui-table-view">';// $("#loadimg").show();// for(var i = 0; i < data.length; i++) {// html += '<li class="mui-table-view-cell mui-media"><a href="Javascript:;">';// html += '<img class="mui-media-object mui-pull-right" src="images/yuantiao.jpg">';// html += '<div class="mui-media-body">';// html += 'test';// html += '<p class="mui-ellipsis">'+ data[i]["Title"]+ '</p>';// html += '</div>';// html += '</a></li>';// }// html += '</ul>';// $('#divCustomers').html(html);// $("#loadimg").hide();//});//function youAlert(data) {// alert(data)//}</script>
ashx后台处理程序
public void PRocessRequest(HttpContext context)
{ context.Response.Clear(); context.Response.ContentType = "application/json"; //这里完美解决ajax跨域请求的问题 HttpContext.Current.Response.AddHeader("access-Control-Allow-Origin", "*"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS,DELETE,PUT"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Test"); string callback = context.Request["callback"]; string strWhere = "1=1"; string Cid = context.Request.QueryString["Cid"]; string page = context.Request.QueryString["page"]; DataTable dt = projectBLL.GetList(strWhere).Tables[0]; string jsonString = DataTableToJSON(Pages(page, Cid)); context.Response.Write(callback + "" + jsonString + ""); }新闻热点
疑难解答