admin管理员组

文章数量:1291138

I am trying to run a function to generate the url used in a jQuery ajax POST request, but it seems the url parameter can only be a string... Here is the code, note the function in the url parameter of the ajax call:

//chained code upstairs...
.bind("create.jstree", function (e, data) {
    $.ajax({
        //"/project/<%= locals.project.handle %>/mkdir/<%=locals.filepath%>",
        //"http://127.0.0.1/project/datanotes/mkdir/lolada/lolada_subdir",
        type : "POST",
        url :   function(data){
            var url = "<%= locals.request.db.baseURI + "/project/" + locals.project.handle + "/mkdir" %>";
            if(data.rslt.obj.attr("id") != null){
                url = url + "/" + data.rslt.obj.attr("id");
            }
            return url;
        },
        data : {
            "title" : data.rslt.name,
            "type" : data.rslt.obj.attr("rel")
        },
        success: function (r) {
            if(r.status) {
                $(data.rslt.obj).attr("id", "node_" + r.id);
            }
            else {
                $.jstree.rollback(data.rlbk);
            }
        },
        failure : function (r) {
            $.jstree.rollback(data.rlbk);
        }
    });
})

The url parameter is interpreted as a string and not evaluated, as the debug log is the following:

GET /project/datanotes/browse/children 200 369ms - 2b
POST /project/function%20(data)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20url%20=%20%22http://127.0.0.1:3000/project/datanotes/mkdir%22;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(data.rslt.obj.attr(%22id%22)%20!=%20null)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20url%20=%20url%20+%20%22/%22%20+%20data.rslt.obj.attr(%22id%22);%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20url;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D 404 5ms

Can you help me circunvent the problem? Thanks!

I am trying to run a function to generate the url used in a jQuery ajax POST request, but it seems the url parameter can only be a string... Here is the code, note the function in the url parameter of the ajax call:

//chained code upstairs...
.bind("create.jstree", function (e, data) {
    $.ajax({
        //"/project/<%= locals.project.handle %>/mkdir/<%=locals.filepath%>",
        //"http://127.0.0.1/project/datanotes/mkdir/lolada/lolada_subdir",
        type : "POST",
        url :   function(data){
            var url = "<%= locals.request.db.baseURI + "/project/" + locals.project.handle + "/mkdir" %>";
            if(data.rslt.obj.attr("id") != null){
                url = url + "/" + data.rslt.obj.attr("id");
            }
            return url;
        },
        data : {
            "title" : data.rslt.name,
            "type" : data.rslt.obj.attr("rel")
        },
        success: function (r) {
            if(r.status) {
                $(data.rslt.obj).attr("id", "node_" + r.id);
            }
            else {
                $.jstree.rollback(data.rlbk);
            }
        },
        failure : function (r) {
            $.jstree.rollback(data.rlbk);
        }
    });
})

The url parameter is interpreted as a string and not evaluated, as the debug log is the following:

GET /project/datanotes/browse/children 200 369ms - 2b
POST /project/function%20(data)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20url%20=%20%22http://127.0.0.1:3000/project/datanotes/mkdir%22;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(data.rslt.obj.attr(%22id%22)%20!=%20null)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20url%20=%20url%20+%20%22/%22%20+%20data.rslt.obj.attr(%22id%22);%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20url;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D 404 5ms

Can you help me circunvent the problem? Thanks!

Share Improve this question edited Sep 26, 2013 at 14:04 Lukasz Koziara 4,3205 gold badges34 silver badges44 bronze badges asked Sep 26, 2013 at 13:02 João Rocha da SilvaJoão Rocha da Silva 4,3094 gold badges27 silver badges36 bronze badges 2
  • 4 Not sure if you can do this or not (what you're trying), but why not run the function right before the AJAX call, assign it to a variable, then use that? – tymeJV Commented Sep 26, 2013 at 13:05
  • Exactly, that is the best solution, thanks! – João Rocha da Silva Commented Sep 26, 2013 at 13:18
Add a ment  | 

2 Answers 2

Reset to default 9

You are passing the function, not executing it and passing the result. You could use an immediately invoked function:

url: (function () {
    var url = "<%= locals.request.db.baseURI + " / project / " + locals.project.handle + " / mkdir " %>";

    if (data.rslt.obj.attr("id") != null) {
        url = url + "/" + data.rslt.obj.attr("id");
    }

    return url;
})(),

Simple demo: http://jsfiddle/yS6rn/

You were expecting a function to be evaluated when only a string was required. Why overplicate it? Just calculate the URL before the AJAX call (but still inside the binding function).

//chained code upstairs...
.bind("create.jstree", function (e, data) {
    var url = "<%= locals.request.db.baseURI + " / project / " + locals.project.handle + " / mkdir " %>";

    if (data.rslt.obj.attr("id") != null) {
        url = url + "/" + data.rslt.obj.attr("id");
    }

    $.ajax({
        //"/project/<%= locals.project.handle %>/mkdir/<%=locals.filepath%>",
        //"http://127.0.0.1/project/datanotes/mkdir/lolada/lolada_subdir",
        type: "POST",

        url: url,

        data: {
            "title": data.rslt.name,
                "type": data.rslt.obj.attr("rel")
        },
        success: function (r) {
            if (r.status) {
                $(data.rslt.obj).attr("id", "node_" + r.id);
            } else {
                $.jstree.rollback(data.rlbk);
            }
        },
        failure: function (r) {
            $.jstree.rollback(data.rlbk);
        }
    });
})

本文标签: javascriptFunction in url parameter in jQuery Ajax RequestStack Overflow