做了一个jquery+json的例子,但有些地方不清楚,请高手指点.
业面上我放置了两个html button,一个读取后台JSONHandler.ashx的方法获取json对象
Code
Code
function loadJSON1()
{
$.getJSON('JSONHandler.ashx', { }, loaded1);
}
function loaded1(result)
{
$.each(result,function(i)
{$('#list').append('<li>[[Get方式]]Name:'+result[i].name+'-Age:'+result[i].age + '</li>');})
}
function loadJSON2(){
$.ajax({
type: 'post',
url: 'jqtest.aspx/LoadData',
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
ifModified: true,
success: loaded2
})
}
function loaded2(result)
{
result = JOSNtoObject(result.d);
$.each(result,
function(i) {
$('#list').append('<li>[[Post方式json.js]]Name:' + result[i].name + '---Age:' + result[i].age + '</li>');})
}
function JOSNtoObject(jsonstring) {
eval("var jsonObject=" + jsonstring);
return jsonObject;
}
Code
function loadJSON1()
{
$.getJSON('JSONHandler.ashx', { }, loaded1);
}
function loaded1(result)
{
$.each(result,function(i)
{$('#list').append('<li>[[Get方式]]Name:'+result[i].name+'-Age:'+result[i].age + '</li>');})
}
function loadJSON2(){
$.ajax({
type: 'post',
url: 'jqtest.aspx/LoadData',
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
ifModified: true,
success: loaded2
})
}
function loaded2(result)
{
result = JOSNtoObject(result.d);
$.each(result,
function(i) {
$('#list').append('<li>[[Post方式json.js]]Name:' + result[i].name + '---Age:' + result[i].age + '</li>');})
}
function JOSNtoObject(jsonstring) {
eval("var jsonObject=" + jsonstring);
return jsonObject;
}
后台方法分别为:
JSONHandler.ashx
Code
<%@ WebHandler Language="C#" Class="JSONHandler" %>
using System;
using System.Web;
using System.Web.Services;
public class JSONHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string data = "[{name:'ants',age:24},{name:'lele',age:23}]";
context.Response.Write(data);
}
public bool IsReusable {
get {
return false;
}
}
}
<%@ WebHandler Language="C#" Class="JSONHandler" %>
using System;
using System.Web;
using System.Web.Services;
public class JSONHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string data = "[{name:'ants',age:24},{name:'lele',age:23}]";
context.Response.Write(data);
}
public bool IsReusable {
get {
return false;
}
}
}
jqtest.aspx
Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Services;
public partial class JQTest_jqtest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string LoadData()
{
string data = "[{\"name\":\"ants\",\"age\":24},{\"name\":\"" + Guid.NewGuid().ToString() + "\",\"age\":25}]";
return data;
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Services;
public partial class JQTest_jqtest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string LoadData()
{
string data = "[{\"name\":\"ants\",\"age\":24},{\"name\":\"" + Guid.NewGuid().ToString() + "\",\"age\":25}]";
return data;
}
}
问题是:调用业面jqtest.aspx的方法,为什么在将返回客户端字符串转换为json 对象时,(客户端loaded2方法)需取result.d呢?是不是跟Content-Type和dataType 的设置有关呢?