我已经实现了一个用这样的代码创建的 jsTree:
}).jstree({
"json_data": {
"ajax": {
"url": '<% = ResolveClientUrl("../GetTree.asmx/FullTree") %>',
"async": true,
"contentType": "application/json; charset=utf-8",
"dataType": "json",
"success": function (data, textStatus, jqXHR) {
alert("Transmission Success.");
alert(data);
var obj = $.parseJSON(data);
alert("Parsing JSON Success.");
// Can I access the return value of the web service FullTree function here?
return data.d;
},
"data": function (n) {
return {
id: nodoIniziale,
level: 0
};
},
},
"progressive_render": true
},
ASP.NET 函数如下:
[WebMethod(EnableSession = true)]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public int MacchineFullTree()
{
List<jsTreeNewNode> nodes = new List<jsTreeNewNode>();
int retValue;
// ==============
// Code that creates the Tree and assign retValue = xxx
// ==============
JavaScriptSerializer serializer = new JavaScriptSerializer();
string nodesSerialized = serializer.Serialize(nodes);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
HttpContext.Current.Response.AddHeader("Content-Length", nodesSerialized.Length.ToString(CultureInfo.InvariantCulture));
HttpContext.Current.Response.AddHeader("Connection", "Close");
HttpContext.Current.Response.Write(nodesSerialized);
HttpContext.Current.Response.Flush();
return retValue;
如何访问 Web 服务返回值?(如果这是有道理的)(更好的是从 Web 服务函数返回一个对象,而不是一个 int 值......;-))
重要编辑
如果我的 Web 服务函数只是:public int MacchineFullTree() { return 9999; }
那么在成功函数中,我可以获得询问data.d的返回值。
但是在我的情况下,我正在“强制”服务器响应HttpContext.Current.Response.Write(nodesSerialized);
并询问data.d
我得到undefined
的。