日期:2014-05-16  浏览次数:20775 次

使用getJSON方法怎么没有任何效果啊,急!! - Web 开发 / Ajax
本人在JQuery方面只是个菜鸟(刚刚接触~~),今天自己尝试着写了段关于getJSON的代码,但是当运行之后却见不到
任何效果,在IE8下调试也无法进行,真的很郁闷,还请各位好友帮忙看看啊~~~
  以下是我的代码: 

aspx页面:[color=#000080][/color]

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="AjaxJson_Default" %>

<!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="../JavaScript/jquery-1.3.2.js" type="text/javascript"></script>

  <script type="text/javascript">
  $(document).ready(function() {
  $.getJSON("Handler.ashx", function(data) {
  alert('aa');
  $('#divList').html(data.id);
  }
  );
  });
  </script>

</head>
<body>
  <form id="form1" runat="server">
  <div id="divList">
  </div>
  </form>
</body>
</html>

Handler.ashx:



<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {
   
  public void ProcessRequest (HttpContext context) {
  context.Response.ContentType = "text/plain";
  context.Response.Write("[{id:1, name:'n_1'}, {id:2, name:'n_2'}]");
  }
 
  public bool IsReusable {
  get {
  return false;
  }
  }

}

------解决方案--------------------
你返回的json对象数组,数组莫id属性啊

JScript code
$('#divList').html(data[0].id);

------解决方案--------------------
context.Response.ContentType = "text/plain"; 

context.Response.Write("[{id:1, name:'n_1'}, {id:2, name:'n_2'}]")

你想得到 json 这个contentType 对吗?
------解决方案--------------------
根据索引来取
------解决方案--------------------
安装火狐浏览器,然后安装火狐调试工具,调试一下看看有没有获取到值,再看处理是否正确。
------解决方案--------------------
你返回的data是对象吗,是对象要json[i].属性
------解决方案--------------------
你先确定data有无值啊。
这样取值的 $('#divList').html(data[0].id);
------解决方案--------------------
第一:确保你的JQuery文件的引用是正确的.(判断这点只需看你的 alert('aa'); 有没弹出. 如果没有则表示引用不正确)
第二:把现实id的命令行改为
$('#divList').html(data[0].id);
就可以了.