在asp.net 2.0 中jquery.treeview如何异步向后台webservice获取json数据???

foshan 2009-04-22 01:27:15
在asp.net 2.0 中jquery.treeview如何异步向后台webservice获取json数据???
下载treeview的DEMO展示异步加载节点是向PHP后台获取数据的,但我改用webservice就不能获取数据。
以下的代码实现不了异步向后台获取数据

<script type="text/javascript" src="../Scripts/treeview/lib/jquery.js"></script>
<script type="text/javascript" src="../Scripts/treeview/lib/jquery.cookie.js"></script>
<script type="text/javascript" src="../Scripts/treeview/jquery.treeview.async.js"></script>
<script type="text/javascript" src="../Scripts/treeview/jquery.treeview.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#black").treeview({
url: "WebService1.asmx/show"
});
});
</script>
但是,以下方式是可以从后台获取数据的,说明WebService是没有问题的。经检测,$("#black").treeview({url: "WebService1.asmx/show"})根本就没有向服务器提出请求,但$("#black").treeview({url: "source.php"})就向服务器提出请求并返回数据。
<input id="Button1" type="button" value="button" onclick="A()"/>

function A() {
var x = $.ajax({ type: "POST",
url: "WebService1.asmx/show",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
后台:
[WebMethod]
public string show()
{
string pStrReply = "[{ \"text\": \"1. Review of existing structures\", \"expanded\": true, \"children\": [ { \"text\": \"1.1 jQuery core\" }, { \"text\": \"1.2 metaplugins\" } ] }, { \"text\": \"2. Wrapper plugins\" }, { \"text\": \"3.Summary\" }, { \"text\": \"4. Questions and answers\" } ]";
return pStrReply;
}
...全文
4298 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
未来的神话 2011-04-14
  • 打赏
  • 举报
回复
确实啊,jquery很强大,可惜方法,对象太多了,难学啊
BenAiDai900720 2010-12-02
  • 打赏
  • 举报
回复
楼主 有这三个文件么,发给我 jquery.treeview.js jquery.treeview.async.js 谢谢,我的邮箱535509030@qq.com
qq373591361 2009-12-24
  • 打赏
  • 举报
回复
原来真的跟js的引用有关。我把它改成

<script type="text/javascript" src="../javascript/jquery.js"></script>
<script type="text/javascript" src="../javascript/jquery-treeview/jquery.treeview.js"></script>
<script type="text/javascript" src="../javascript/jquery-treeview/jquery.treeview.async.js"></script>

就可以了。
岁月之梦 2009-09-03
  • 打赏
  • 举报
回复
我的请求source.php 都不请求,你的还成功了!不可思议!你要是解决了 告诉我啊!
alphamixture 2009-07-27
  • 打赏
  • 举报
回复
<script type="text/javascript" src="../Scripts/treeview/lib/jquery.js"> </script>
<script type="text/javascript" src="../Scripts/treeview/lib/jquery.cookie.js"> </script>
<script type="text/javascript" src="../Scripts/treeview/jquery.treeview.async.js"> </script>
<script type="text/javascript" src="../Scripts/treeview/jquery.treeview.js"> </script>
顺序错了,引入jquery以后,顺次应该是treeview的js,你可以试试,把那个放在第二位
xingshilinjack 2009-04-24
  • 打赏
  • 举报
回复
学习AJAX,发现很强大
foshan 2009-04-24
  • 打赏
  • 举报
回复
谢谢楼上朋友的解答,但以下代码在运行时根据没有向后台提出请求,不知错在哪里?
前台:

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

<!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>
<link href="../Scripts/treeview/jquery.treeview.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../Scripts/treeview/lib/jquery.js"></script>
<script type="text/javascript" src="../Scripts/treeview/lib/jquery.cookie.js"></script>
<script type="text/javascript" src="../Scripts/treeview/jquery.treeview.async.js"></script>
<script type="text/javascript" src="../Scripts/treeview/jquery.treeview.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$("#ajaxTree").treeview({ url: "TreeViewHandler.ashx" });
});
</script>

</head>
<body>
<ul id="ajaxTree">
</ul>
</body>
</html>


后台:
<%@ WebHandler Language="C#" Class="TreeViewHandler" %>

using System;
using System.Web;

public class TreeViewHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
HttpResponse response = context.Response;
HttpRequest request = context.Request;

response.ContentType = "application/json; charset=utf-8";

string json = string.Empty;
string param = GetQueryString(request, "root");
if (param == "source")
{
json = "[{ \"text\": \"1. Review of existing structures\", \"expanded\": true, \"children\": [ { \"text\": \"1.1 jQuery core\" }, { \"text\": \"1.2 metaplugins\" } ] }, { \"text\": \"2. Wrapper plugins\" }, { \"text\": \"3.Summary\" }, { \"text\": \"4. Questions and answers\" } ]";

}
else
{
json = "[{ \"text\": \"1. Review of existing structures\", \"expanded\": true, \"children\": [ { \"text\": \"1.1 jQuery core\" }, { \"text\": \"1.2 metaplugins\" } ] }, { \"text\": \"2. Wrapper plugins\" }, { \"text\": \"3.Summary\" }, { \"text\": \"4. Questions and answers\" } ]";
}
response.Write(json);

}
private string GetQueryString(HttpRequest request, string key)
{
return (request.QueryString[key] != null) ? request.QueryString[key] : string.Empty;
}
public bool IsReusable
{
get {
return false;
}
}

}
foshan 2009-04-24
  • 打赏
  • 举报
回复
谢谢楼上朋友的解答,但以下代码在运行时根据没有向后台提出请求,不知错在哪里?
前台:

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

<!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>
<link href="../Scripts/treeview/jquery.treeview.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../Scripts/treeview/lib/jquery.js"></script>
<script type="text/javascript" src="../Scripts/treeview/lib/jquery.cookie.js"></script>
<script type="text/javascript" src="../Scripts/treeview/jquery.treeview.async.js"></script>
<script type="text/javascript" src="../Scripts/treeview/jquery.treeview.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$("#ajaxTree").treeview({ url: "TreeViewHandler.ashx" });
});
</script>

</head>
<body>
<ul id="ajaxTree">
</ul>
</body>
</html>


后台:
<%@ WebHandler Language="C#" Class="TreeViewHandler" %>

using System;
using System.Web;

public class TreeViewHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
HttpResponse response = context.Response;
HttpRequest request = context.Request;

response.ContentType = "application/json; charset=utf-8";

string json = string.Empty;
string param = GetQueryString(request, "root");
if (param == "source")
{
json = "[{ \"text\": \"1. Review of existing structures\", \"expanded\": true, \"children\": [ { \"text\": \"1.1 jQuery core\" }, { \"text\": \"1.2 metaplugins\" } ] }, { \"text\": \"2. Wrapper plugins\" }, { \"text\": \"3.Summary\" }, { \"text\": \"4. Questions and answers\" } ]";

}
else
{
json = "[{ \"text\": \"1. Review of existing structures\", \"expanded\": true, \"children\": [ { \"text\": \"1.1 jQuery core\" }, { \"text\": \"1.2 metaplugins\" } ] }, { \"text\": \"2. Wrapper plugins\" }, { \"text\": \"3.Summary\" }, { \"text\": \"4. Questions and answers\" } ]";
}
response.Write(json);

}
private string GetQueryString(HttpRequest request, string key)
{
return (request.QueryString[key] != null) ? request.QueryString[key] : string.Empty;
}
public bool IsReusable
{
get {
return false;
}
}

}
zming 2009-04-23
  • 打赏
  • 举报
回复
webservice必须要通过post方式才能访问,jquery.treeview很可能是通过get方式发出的请求
zming 2009-04-23
  • 打赏
  • 举报
回复
改用 HttpHandler 处理程序
js

<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#ajaxTree").treeview({ url: "TreeViewHandler.ashx" });
});
</script>


HttpHandler, 代码中使用了 Newtonsoft.Json, JQueryTreeNode 是自定义实体类

public void ProcessRequest (HttpContext context) {
HttpResponse response = context.Response;
HttpRequest request = context.Request;

response.ContentType = "application/json; charset=utf-8";

string json = string.Empty;
string param = GetQueryString(request, "root");
if (param == "source")
{
JQueryTreeNode node = new JQueryTreeNode(1, "Root");
IList<JQueryTreeNode> children = new List<JQueryTreeNode>();
children.Add(new JQueryTreeNode(11, "One"));
children.Add(new JQueryTreeNode(12, "Two", true));
children.Add(new JQueryTreeNode(13, "Three"));

node.children = children;

json = JavaScriptConvert.SerializeObject(node);
json = string.Format("[{0}]", json);
}
else
{
int id = 0;
if (int.TryParse(param, out id))
{
IList<JQueryTreeNode> children = new List<JQueryTreeNode>();
children.Add(new JQueryTreeNode(21, "22222222"));
//children.Add(new JQueryTreeNode(22, "3333333", true));
//children.Add(new JQueryTreeNode(23, "44444444444"));

json = JavaScriptConvert.SerializeObject(children);
}
}

response.Write(json);
}

private string GetQueryString(HttpRequest request, string key)
{
return (request.QueryString[key] != null) ? request.QueryString[key] : string.Empty ;
}
foshan 2009-04-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zming 的回复:]
webservice必须要通过post方式才能访问,jquery.treeview很可能是通过get方式发出的请求
[/Quote]

应该如何处置啊?拜托!

52,782

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧