• jquery ajax




    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        
    <title></title>
        
    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
       
    <script type="text/javascript">
           
    function LoadData() {

               
    var data = "";
               $(
    "#alldata").html("");
          $.getJSON(
    "Handler1.ashx"""function (json) {

                   
    /*
                   $.each(json,function(i){
                   $("#alldata").append("<li>城市:"+json[i].city+"&nbsp; 面积:"+json[i].mianji+"</li>");
                   });
                   
    */
                   
    for (var j = 0; j < json.length; j++) {
                       $(
    "#alldata").append("<li>城市:" + json[j].city + "&nbsp; 面积:" + json[j].mianji + "</li>");
                   }
                       })


           }
           
    function LoadDataajax() {
               $(
    "#alldata").html("");
               $.ajax({
                   type: 
    "get",
                   url: 
    "Handler1.ashx",
                   data: 
    "",
                   success: 
    function (json) {
                  
    var jsondata = eval(json);
                       
    for (var j = 0; j < jsondata.length; j++) {
                           $(
    "#alldata").append("<li>城市:" + jsondata[j].city + "&nbsp; 面积:" + jsondata[j].mianji + "</li>");
                       }
                }
               });
               
               
           
           }
       
    </script>
    </head>

    <body >
        
    <form id="form1" runat="server">
       
        
    <div>
          
    <ul id="alldata"></ul>
          
    <input type="button" value="获取数据"  onclick="LoadData()"/>
          
    <input type="button" value="ajax 获取数据" onclick="LoadDataajax()" />
        
    </div>
        
    </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace WebApplication2
    {
        
    /// <summary>
        
    /// Summary description for Handler1
        
    /// </summary>
        public class Handler1 : IHttpHandler
        {

            
    public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType 
    = "text/plain";
                
    string jsondata = @"[
                              {'city':'北京','mianji':16800,'renkou':1600},
                              {'city':'上海','mianji':6400,'renkou':1800}
                              ]
    ";
                context.Response.Write(jsondata);
            }

            
    public bool IsReusable
            {
                
    get
                {
                    
    return false;
                }
            }
        }
    }
  • 相关阅读:
    ADO.NET朝花夕拾(二)
    使文本框自动适应内容的高度
    jQuery getJSON
    ASP.NET页面生命周期概述
    CSS学习(四)CSS选择符详解
    jQuery dialog 异步调用ashx,webservice数据
    jQuery之小议each()
    jQuery,Ashx发送站内信
    Newtonsoft.Json处理日期问题
    CSS学习(三)带当前标识的横向导航图片美化版
  • 原文地址:https://www.cnblogs.com/gwazy/p/1590791.html
Copyright © 2020-2023  润新知