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
2 Answers
Reset to default 9You 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
版权声明:本文标题:javascript - Function in url parameter in jQuery Ajax Request - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741512601a2382693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论