moluxiaoy
moluxiaoy
采纳率0%
2016-06-29 03:54 阅读 2.7k

jsTree ajax应该怎么写

$(function() {

$("#mechanismtree").jstree(

{

"json_data" : {

"ajax" : {

"url" :${pageContext.request.contextPath }+"/manage/mechanism/selsetMechanismtree", ///${pageContext.request.contextPath }/manage/user/mechanism_manage.jsp

                    "data" : function(n) {                              //http://localhost:8080/trainexam/manage/mechanism/selsetMechanismtree
                                // the result is fed to the AJAX request `data` option  
                                return {  
                                    "operation" : "get_children",  
                                    "id" : n.attr ? n.attr(  
                                        "id")  
                                        .replace(  
                                        "node_",  
                                        "")  
                                        : 1  
                                };  
                            }  
                        }  
                    },  

            "plugins" : [  
                        "themes",  
                        "json_data",  

                        "ui",  
                        "crrm",  
                        "contextmenu",  
                        "checkbox",
                        "search" ],  

                })  

        .bind("loaded.jstree",  
                function(event, data) {  
                })  

        .bind(  
                "select_node.jstree",  
                function(event, data) {  
                    if (data.rslt.obj  
                            .attr("id") != undefined) {  
                    }  
                })  
        .bind(  
                "remove.jstree",  
                function(e, data) {  
                    data.rslt.obj.each(function() {  
                            $.ajax({  
                                    async : false,  
                                    type : 'POST',  
                                    url : "http://localhost:8080/MemberManager/CreateNodeForDepartment",  
                                    data : {  
                                        "operation" : "remove_node",  
                                        "id" : this.id.replace("node_", "")  
                                    },  
                                    success : function(r) {  
                                        if (!r.status) {  
                                            data.inst.refresh();  
                                        }  
                                    }  
                                });  
                        });  
                })  

        .bind(  
                "create.jstree",  
                function(e, data) {  
                    $.post(  
                        "http://localhost:8080/MemberManager/CreateNodeForDepartment",  
                        {  
                            "operation" : "create_node",  
                            "id" : data.rslt.parent  
                                .attr(  
                                "id")  
                                .replace(  
                                "node_",  
                                ""),  
                            "position" : data.rslt.position,  
                            "title" : data.rslt.name,  
                            "type" : data.rslt.obj  
                                .attr("rel")  
                        },  
                        function(r) {  
                            if (r.status) {  
                                $(data.rslt.obj).attr("id", "node_" + r.id);  
                            } else {  
                                data.inst.refresh();  
                                $.jstree.rollback(data.rlbk);  
                            }  
                        });  
                })  
        .bind(  
                "rename.jstree",  
                function(e, data) {  
                    $.post(  
                        "http://localhost:8080/MemberManager/CreateNodeForDepartment",  
                        {  
                            "operation" : "rename_node",  
                            "id" : data.rslt.obj  
                                .attr(  
                                "id")  
                                .replace(  
                                "node_",  
                                ""),  
                            "title" : data.rslt.new_name  
                        },  
                        function(r) {  
                            if (!r.status) {  
                                data.inst.refresh();  
                                $.jstree.rollback(data.rlbk);  

                            }  
                        });  
                })  
                // 1) the loaded event fires as soon as data is parsed and inserted  

                // 2) but if you are using the cookie plugin or the core `initially_open` option:  
        .one("reopen.jstree",  
                function(event, data) {  
                })  
                // 3) but if you are using the cookie plugin or the UI `initially_select` option:  
        .one("reselect.jstree",  
                function(event, data) {  
                });  
        });  

这是我写的代码 我有如下两个问题需要各位大神帮忙解决啊,我是新手查了很多资料 也没有弄明白
1.jsTree的初始化数据为什么如图1这样写 ajax的代码怎么会写成 json格式?这样怎么会向后台发送数据呢?ajax不是应该是如下的写法么?

function mechanismSearch(no) {
        searchOpts.no = no;
        //alert("hhhhh");
        $.ajax({
                    url : ctx+"/manage/mechanism/selsetMechanism",
                    type : "POST",
                    data : searchOpts,
                    dataType : "json",
                    success : function(result) {
                    。。。。。。。。。。。。。。。。。。。。。。。。。。
                    }
                    ![图片说明](https://img-ask.csdn.net/upload/201606/29/1467172189_64847.png)ajax这里是json格式 我有点没搞懂?
                    2.像这样ajax代码写成json格式 那么我写请求路径的时候项目的路径怎么写?请看下面的代码 以前写路径都是这么写的。
                <c:set var="ctx" value="${pageContext.request.contextPath}" />


                $.ajax({
                    url : ctx+"/manage/mechanism/selsetMechanism",
                    type : "POST",
                    data : searchOpts,
                    dataType : "json",
                    success : function(result) {......
                    ......}


                    请各位大神们详细给我说说吧!困扰我好久了,谢谢各位!
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐