Anu*_*Anu 4 javascript web-services
我在.aspx页面上从客户端调用Web服务,我想调用一个关于此服务成功的函数.
函数名称将作为参数传递给此函数,该函数将动态更改.
我这样传递:
function funName parm1, parm2, onSucceedCallFuntion
function onSucceedCallFuntion(result)
//doing something here.
Run Code Online (Sandbox Code Playgroud)
也许是因为它是一个字符串是无法调用"成功"函数的原因
function funName(parm1, par2, onSucceedFunName) {
$.ajax({
url: "../WebServices/ServiceName.asmx/ServiceFunName",
data: JSON.stringify({
parm1: parm1,
par2: par2
}), // parameter map type: "POST", // data has to be POSTED
contentType: "application/json",
dataType: "json",
success: onSucceedFunName,
});
function onSucceedFunName() {}
Run Code Online (Sandbox Code Playgroud)
sdl*_*rhc 15
如果您将函数的名称作为字符串传递,您可以尝试这样做:
window[functionName]();
Run Code Online (Sandbox Code Playgroud)
但这假设功能在全球范围内.另一种更好的方法是只传递函数本身:
function onSuccess() {
alert('Whoopee!');
}
function doStuff(callback) {
/* do stuff here */
callback();
}
doStuff(onSuccess); /* note there are no quotes; should alert "Whoopee!" */
Run Code Online (Sandbox Code Playgroud)
编辑
如果需要将变量传递给函数,可以将它们与函数一起传递.这就是我的意思:
// example function
function greet(name) {
alert('Hello, ' + name + '!');
}
// pass in the function first,
// followed by all of the variables to be passed to it
// (0, 1, 2, etc; doesn't matter how many)
function doStuff2() {
var fn = arguments[0],
vars = Array.prototype.slice.call(arguments, 1);
return fn.apply(this, vars);
}
// alerts "Hello, Chris!"
doStuff2(greet, 'Chris');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9835 次 |
| 最近记录: |