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;
            }
        }
    }
}

 

posted on 2009-10-27 17:05  gwazy  阅读(193)  评论(0编辑  收藏  举报

导航