当onFailure调用-${remoteFunction-Grails时

当onFailure调用-${remoteFunction-Grails时,grails,gsp,Grails,Gsp,我的代码是: ${remoteFunction(controller:'user', action:'clearCache', params:'\'&cacheUserId=\' + cacheUserId + \'&pageName=\' + pageName', onSuccess: 'onSuccess()', onFailure: 'onFailure()') }; 在我的控制器操作方法中 clearCache(){ def status = getBusi

我的代码是:

${remoteFunction(controller:'user', action:'clearCache', 
params:'\'&cacheUserId=\' + cacheUserId + \'&pageName=\' + pageName', 
onSuccess: 'onSuccess()', 
onFailure: 'onFailure()') };
在我的控制器操作方法中

clearCache(){

  def status = getBusiness(); // this method returns boolean only.
  if(status==true){
  render "Success"}
  else render "Failure"
}
新的,它总是调用onSuccess()javascript函数

所以我删除了else块,然后它在状态为false时不调用onFailue/onSuccess

我能知道onFailure何时调用吗?如果状态为false,我如何调用它。

方法

onSuccess (optional) - The JavaScript function to call if successful
意味着如果控制器成功(如本代码所示),则调用此方法

onFailure (optional) - The JavaScript function to call if the call fails
这意味着,如果控制器出现故障(示例中的错误404),则调用此方法

onFailure (optional) - The JavaScript function to call if the call fails
这就是为什么在代码中总是调用onSuccess

解决方案我认为您可以传递到onSuccess函数数据参数:

<script type="text/javascript">
    function onSuccess(data,textStatus){
        alert(data)

        }

    </script>

函数onSuccess(数据、文本状态){
警报(数据)
}
在数据中,您可以找到呈现的响应

onSuccess (optional) - The JavaScript function to call if successful
意味着如果控制器成功(如本代码所示),则调用此方法

onFailure (optional) - The JavaScript function to call if the call fails
这意味着,如果控制器出现故障(示例中的错误404),则调用此方法

onFailure (optional) - The JavaScript function to call if the call fails
这就是为什么在代码中总是调用onSuccess

解决方案我认为您可以传递到onSuccess函数数据参数:

<script type="text/javascript">
    function onSuccess(data,textStatus){
        alert(data)

        }

    </script>

函数onSuccess(数据、文本状态){
警报(数据)
}

在数据中,您可以找到呈现的响应。在发生“异常”时,您应该更改控制器代码以显式发送适当的HTTP状态代码。该方法允许通过
status
属性执行此操作

clearCache(){
    def status = getBusiness(); // this method returns boolean only.
    if(status==true){
        render "Success" // implicit status of 200
    }
    else render(status: 403, text: "Failure")
}
由于不清楚本例中发生的故障类型,我不确定要指定哪种状态代码,因此我选择了403,但您需要阅读并根据您的情况选择最佳状态代码


正如@alessandro所说,
onSuccess
由2XX HTTP状态代码触发,大多数其他代码将触发
onFailure
回调。

当出现“异常”时,您应该更改控制器代码以显式发送适当的HTTP状态代码发生。该方法允许通过
status
属性执行此操作

clearCache(){
    def status = getBusiness(); // this method returns boolean only.
    if(status==true){
        render "Success" // implicit status of 200
    }
    else render(status: 403, text: "Failure")
}
由于不清楚本例中发生的故障类型,我不确定要指定哪种状态代码,因此我选择了403,但您需要阅读并根据您的情况选择最佳状态代码


正如@alessandro所说,
onSuccess
由2XX HTTP状态代码触发,大多数其他代码将触发
onFailure
回调。

我尝试过,但javascript控制台sais的数据未定义;我尝试过,但javascript控制台sais的数据未定义