Javascript函数不返回值 [英] Javascript function not returning value

查看:106
本文介绍了Javascript函数不返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码调用函数:

I am calling a function with the following code:

var resultz = nsEditor.updateStringCall(debtID, column2Change, value2Change, 4, 10);

正在调用但不返回值的函数

The function that is being called but doesn't return a value

        updateStringCall: function(pdebtID, pcolumn2Change, pvalue2Change, pmin, pmax){
try {
    var resultz2 = 0;
    $.ajax({
        type: "POST",
        url: "Components/MintLibraries.cfc",
        dataType: "json",
        cache: false,
        data: {
                method: 'StringUpdate',
            val2Change: pvalue2Change.trim(),
            debtID: pdebtID,
            column2Change: pcolumn2Change,
            greaterThan: pmin,
            lesserThan: pmax,
            returnFormat: "json"
        },
        success: function(response) {
                       resultz2 = 1;
          return resultz2;
        },  //end done
        error: function(jqXHR, textStatus, errorThrown){
           resultz2 = 0;
                       return resultz2;
                }); //end ajax call
} catch (e) {
    resultz2 = 0;
    return resultz2;
}  //end trycatch

},// end updateStringCall

}, // end updateStringCall

此函数使用.ajax调用coldfusion cfc方法StringUpdate:

This function uses the .ajax to call a coldfusion cfc method StringUpdate:

<cffunction name="StringUpdate" access="remote" output="false" returntype="any" >    
     <cfargument name="val2Change" type="string"  required="true" />
     <cfargument name="debtID"  type="string"  required="true" />
     <cfargument name="column2Change"  type="string"  required="true" />
     <cfargument name="greaterThan"  type="numeric"   required="false" />
     <cfargument name="lesserThan"  type="numeric"  required="false" />
     <cfset var debt2Pass = int(val(arguments.debtID)) />
     <cfset var updQuery = "" />
     <cfset var retValue = 0 />
     <cfset var iVal = 0 /><cfset var valError = '' />
    <cftry>

        <cfquery name="updQuery" datasource="#application.datasource#" result="qResults"    >
                Update dmf set #arguments.column2Change# =
                 <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.val2Change#"/>
                 where DebtID = 
                 <cfqueryparam cfsqltype="cf_sql_smallint" value="#arguments.debtID#"/>
        </cfquery> 
        <cfset retValue = 1 />    
     <cfcatch type="Any" >
                <cfset thrown = ThrowCFError(405,'debug',  'Error updating ' &  arguments.DebtID,arguments.val2Change & ', ' &  arguments.debtID & ',' & arguments.column2Change)  />
                  <cfset retValue = 0 />
         </cfcatch>
         </cftry>
    <cfreturn retValue />
</cffunction>

查询成功运行,但js函数不返回值(显示为undefined) 。即使它得到一个错误,我会认为我已经足够了,至少获得一个返回值。

The query runs successfully but the js function doesn't return a value (it shows up as undefined). Even if it gets an error, I would think I have accounted enough for that to at least get a return value.

想法?

推荐答案

函数不返回值,因为 return 你的方法嵌入回调函数中。它们只是从那些回调函数返回,而不是从你的主函数返回。例如,考虑你的成功回调:

The function is not returning a value because the returns in your method are embedded inside callback functions. They are only returning from those callbacks, not from your main function. Consider, for example, your "success" callback:

success: function(response) {
    resultz2 = 1;
    return resultz2;
}

return 只是从成功函数返回...它不会起泡到你的 updateStringCall 函数。

This return is just returning from the success function... It doesn't bubble up to your updateStringCall function.

它必须以这种方式工作的原因是因为ajax调用是异步的。您的方法会立即返回 ,而请求在后台执行。所以为了得到返回值,你必须传入一个回调,它可以访问的值,当它准备好,然后调用回调从ajax回调,而不是返回一个值。

The reason it has to work this way is because ajax calls are asynchronous. Your method returns immediately while the request happens in the background. So in order to get the return value, you have to pass in a callback that has access to the value when it's ready, and then call the callback from within the ajax callbacks instead of returning a value.

所以改变你的方法定义为:

So change your method definition to this:

// Note I added a "callback" parameter
function(pdebtID, pcolumn2Change, pvalue2Change, pmin, pmax, callback) { ... }

然后在你的ajax回调中,调用它而不是返回一个值:

And then in your ajax callbacks, call it instead of returning a value:

success: function(response) {
    resultz2 = 1;
    callback(resultz2);
},
error: function(jqXHR, textStatus, errorThrown) {
    resultz2 = 0;
    callback(resultz2);
});

最后,当您调用此方法时传递回调:

Finally, pass in a callback when you call this method:

nsEditor.updateStringCall(debtID, column2Change, value2Change, 4, 10, function(resultz) {
    // Do something with resultz
});

最后一个注意事项,如果你要在那里有一个try / catch使用回调也:

One final note, if you are going to have that blanket try/catch in there, you should change that to use the callback also:

catch (e) {
    resultz2 = 0;
    callback(resultz2);
}

但实际上,我建议只是尝试/ catch。我不能看到这可能是有帮助的在这里。它只会隐藏已经 的代码具有更好的错误处理结构的实际问题。我怀疑你只是在那里调试这个返回问题,但如果它已经有,只是把它拿出来。

But really, I'd recommend just taking that try/catch out altogether. I can't see how that can possibly be helpful here. It will only hide actual problems with code that already has a better structure for error handling. I suspect you just put in there to debug this return problem, but if it was there already, just take it out.

这是世界的event-驱动函数编程!这是javascript中非常常见的结构。

这篇关于Javascript函数不返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆