jsTree ajax - 从被调用的Web服务函数中获取返回值

时间:2013-09-17 13:02:07

标签: javascript jquery asp.net ajax jstree

我已经实现了一个用这样的代码创建的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值...; - ))

重要编辑

如果我的网络服务功能只是:public int MacchineFullTree() { return 9999; } 然后在成功功能中,我可以获得询问 data.d 的返回值。

但在我的情况下,我使用HttpContext.Current.Response.Write(nodesSerialized);“强制”服务器响应并询问data.d我获得undefined

1 个答案:

答案 0 :(得分:0)

我找到了这个可行且简单的解决方案;-) 我没有使用函数的返回值,而是“增强”了一些树节点, 在根节点中,我将该值添加为节点istelf:

的属性
rootNode.retValue = xxx

(当然这样做我修改了jsTreeNode类。)

缺点:现在全部树的节点都有这个属性,所以服务器响应的长度变大了