2012-12-14 68 views
0

我已經添加了一個用戶名檢查可用函數到我的jquery驗證頁面。如果我在Firebug中查看遠程函數的響應,我會看到正確的響應 - TRUE或FALSE。同樣,如果我在響應消息中添加alert(),它將顯示正確。但是,使用下面的代碼 - addmethod總是返回TRUE。jQuery驗證addmethod總是返回true

$.validator.addMethod('usernamecheck',function(username) { 
    var postURL = [url to remote function] 
    $.ajax({ 
     cache:false, 
     async:false, 
     type: "POST", 
     data: "username=" + username, 
     url: postURL, 
      success: function(msg) { 
       result = (msg=='FALSE') ? false : true; 
      } 
    }); 
    return result; 
}, ''); 

我驗證過的函數返回布爾真或假的唯一但我難倒,爲什麼結果會返回每次true

解決

我切換了CFC,以確保它被設置爲返回布爾樸素:

<cffunction name="checkUsernameRemote" access="remote" returntype="boolean" output="no" returnformat="plain"> 

然後做了一個序列化JSON我的返回值:

<cfset LOCAL.result = SerializeJSON(LOCAL.result)/>  
<cfreturn LOCAL.result /> 

在我的jQuery我擺脫了addMethod()並將其移到遠程規則中:

username: { 
    required: true, 
    rangelength: [4,50], 
    remote: { 
     cache:false, 
     async:false, 
     url: compath + "/rmtUserCFC.cfc?method=checkUsernameRemote&returnformat=json", 
     type: "post" 
    } 
}, 

回答

1

嘗試在success函數中添加您的回報。

success: function(msg) { 
      return (msg=='FALSE') ? false : true; 
     } 
+0

這樣做每次都會返回false。如果我把警報(味精);在返回上面,我從函數返回TRUE或FALSE - 沒有別的。 – Steve

+0

你的底層PHP腳本是什麼樣的?它是否如預期那樣在字符串「FALSE」中回顯,如果在腳本中爲false? – sbeliv01

+0

直接使用函數的輸出,以及螢火蟲顯示功能正確返回true/false基於用戶名檢查。 – Steve

0

我切換了CFC,以確保它被設置爲返回布爾樸素:

<cffunction name="checkUsernameRemote" access="remote" returntype="boolean" output="no" returnformat="plain"> 

然後做了一個序列化JSON我的返回值:

<cfset LOCAL.result = SerializeJSON(LOCAL.result)/>  
<cfreturn LOCAL.result /> 

在我的jQuery我擺脫了addMethod()並將其移入遠程規則中:

username: { 
required: true, 
rangelength: [4,50], 
remote: { 
    url: compath + "/rmtUserCFC.cfc?method=checkUsernameRemote&returnformat=json", 
    type: "post" 
    } 
},