[英]jsTree ajax - Get the return value from called web service function
我已经实现了使用以下代码创建的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; }
public int MacchineFullTree() { return 9999; }
然后在成功的功能,我能得到的返回值询问data.d。
但是,在我的情况下,我使用HttpContext.Current.Response.Write(nodesSerialized);
“强制”服务器响应HttpContext.Current.Response.Write(nodesSerialized);
和询问data.d
我undefined
。
我找到了这个可行且简单的解决方案;-)而不是使用函数的返回值,而是“增强”了更多树节点,并在根节点中将值添加为节点istelf的属性:
rootNode.retValue = xxx
(当然,我已经修改了jsTreeNode类)。
缺点:现在树的所有节点都具有此属性,因此服务器响应的长度变大了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.