我完全被這個問題困住了,而且我很樂意就我做錯了什麼或者我能做些什麼來調試問題。jquery驗證引擎ajax字段驗證失敗
我有jquery驗證引擎驗證表單。 例如,檢查屏幕名稱是否存在的代碼如下所示;
<input value="[% fields.screenname | html %]" placeholder="Screenname"
onclick="$('#screenname').validationEngine('showPrompt', 'For example: JohnDoe_68.', 'load')"
class="validate[required,custom[onlyLetterNumber],maxSize[41],ajax[ajaxNameCallPerlScreenname]]"
name="screenname" id="screenname" type="text" size= "41" maxlength="24" data-prompt-position="centerRight"/>
來驗證引擎的調用是
jQuery(document).ready(function(){
jQuery("#register").validationEngine('attach', 'autoHidePrompt', {
promptPosition : "topRight",
scroll: "false",
ajaxFormValidation : "true",
onBeforeAjaxFormValidation: "beforeCall",
onAjaxFormComplete: "ajaxValidationCallback"
});
});
在這個階段,beforeCall和ajaxValidationCallback功能是直接從演示,反正從來沒有得到所謂所以我不會把它們放在這裏。
在服務器端,我有一個簡單的腳本檢查,以查看該用戶名是否被採用,並通過用Perl編寫的腳本返回json(utf8編碼)。相關的代碼是;
use utf8;
my $output;
eval { $output = JSON::XS->new->utf8->encode($data); };
if([email protected])
{
use Carp;
confess([email protected]);
}
warn "Form Output is " . $output;
$self->discard_request_body();
$self->req->content_type('application/json; charset=utf-8');
$self->req->headers_out->set('Expires' => 'Thu, 1 Jan 1970 00:00:00 GMT');
$self->req->headers_out->set('Content-Length' => length $output);
$self->req->print($output);
這就是麻煩開始的地方。 在鉻和Firefox我可以看到驗證請求擊中服務器, 我可以看到正確的響應得到發送回瀏覽器, 我可以看看瀏覽器中的反應是正確的(我認爲),並且像這樣
[["screenname","false","That user name is already taken. Please try again."]]
但是實際的onAjaxFormComplete永遠不會被調用。如果我在beforeCall和ajaxValidationCallback函數中設置斷點,它們永遠不會被調用。
標題如下;
Response Headers
Connection Keep-Alive
Content-Encoding gzip
Content-Length 89
Content-Type application/json; charset=utf-8
Date Sat, 19 May 2012 19:47:28 GMT
Expires Thu, 1 Jan 1970 00:00:00 GMT
Keep-Alive timeout=15, max=100
Server Apache/2.2.16 (Debian)
Vary User-Agent,Accept-Encoding
X-UA-Compatible chrome=1
X-What-Alias flooting
Request Headers
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection keep-alive
Cookie flooting_session=be8aec0e39f12ee031c9103072b19a66
Host six.flooting.com
Referer http://six.flooting.com/register
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:12.0) Gecko/20100101 Firefox/12.0
X-Requested-With XMLHttpRequest
這就是我現在可以告訴你的一切。我認爲我在配置回調函數的方式上錯過了一些非常簡單的事情,但是我已經在一段時間內對此感到頭痛,而且我沒有更接近答案。
注意beforeCall不在ajax請求之前調用,但是我在代碼的適當部分(應該調用beforeCall的地方)在validationEngine中設置了斷點,並且執行永遠不會達到它。甚至沒有把它交給父功能。
任何人都能看到我失蹤的東西嗎?
尼斯找到馬克 - 但你的例子也有缺陷,因爲你還沒有實現你的回調。你只是在這裏傳遞字符串。你能否更新代碼示例? – IcedDante