In this code :
if (!parms.script) { // no script... load filename
execscript(parms, function (data){
var text={'result':'success', 'response':data };
if(typeof(data)!='object') {
try {
text.response=JSON.parse(text.respon开发者_开发百科se);
} catch(e) {
text={'result':'success','response':data};
}
}
responsehttp.end(JSON.stringify(text));
});
} else {
//parameterised input will replace in script
if(query.paraminput!=undefined) {
var paraminput=qs.parse(query.paraminput);
parms=merge_options(parms, paraminput);
}
execscript(parms, function (data){
var text={'result':'success', 'response':data };
if(typeof(data)!='object') {
try {
text.response=JSON.parse(text.response);
} catch(e) {
text={'result':'success','response':data};
}
}
responsehttp.end(JSON.stringify(text));
});
}
in execscript callback it is called two times , i want to make a single function for executing both call back in if and else .
how can i achieve this .
i tried making seperate function but responsehttp undefinded error camed.
The problem is (I suspect) is that you had your common function declared outside the context of your http response handler and, therefore, responsehttp wasn't defined. Instead create it as a closure function inside your outer function's scope:
function someCallbackYouDontShowInYourQuestion(req, responsehttp) {
// Declare your callback as a closure inside this function, so it
// captures responsehttp state
function callback(data) {
var text={'result':'success', 'response':data };
if(typeof(data)!='object') {
try {
text.response=JSON.parse(text.response);
} catch(e) {
text={'result':'success','response':data};
}
}
responsehttp.end(JSON.stringify(text));
}
if (!parms.script) { // no script... load filename
execscript(parms, callback);
} else {
//parameterised input will replace in script
if(query.paraminput!=undefined) {
var paraminput=qs.parse(query.paraminput);
parms=merge_options(parms, paraminput);
}
execscript(parms, callback);
}
}
callback = function (data){
var text={'result':'success', 'response':data };
if(typeof(data)!='object') {
try {
text.response=JSON.parse(text.response);
} catch(e) {
text={'result':'success','response':data};
}
}
responsehttp.end(JSON.stringify(text));
}
execscript(parms, callback);
精彩评论