[英]jquery - return value from callback function (in post request) into the function its inside of?
我有一個 javascript function 將數據發布到驗證腳本並從那里獲取一個值。 The callback function on the post request returns a boolean value, and I'm trying to get the entire function to return that boolean value. 現在,回調 function 返回正確的值,但 function 本身不返回任何內容。 這是代碼:
function validate(request_type, request_text) {
$.post("http://www.example.com/ajax/validate.php",{
type: request_type,
text: request_text
}, function(data) {
return (data == "valid");
});
}
我意識到這是一種“同步”調用,這不是 AJAX 的意義所在,但我已經在 validate.php (數據庫調用等)中有很多功能,我無法在 Z9E13B69D1D2DAAACA937Z102 和鋸木工像這樣的線程談論使用某種形式的處理程序。
當我在if/else
語句中使用它時,我將如何編寫一個簡單的處理程序來使變量data
或 boolean 比較data == "valid"
的結果可用(這是應該使用 function 的地方) ?
編輯:例如,將使用 boolean 結果的if
語句之一:
if (!validate('password',pass_new)) {
$('#pass_new').addClass('error');
$('#pass_confirm_new').addClass('error');
$(error_string.format('Please enter a valid password.')).insertAfter('#pass_confirm_new');
$('#pass_text_short').hide();
$('#pass_text_long').show();
編輯: function 在我的 HTML 表單中使用onsubmit
事件調用:
function valid_pass_sett() {
//code to remove errors left over from previous submissions - snipped
pass_old = $('input[name=pass_old]').val();
pass_new = $('input[name=pass_new]').val();
pass_confirm_new = $('input[name=pass_confirm_new]').val();
//some if statements that don't involve AJAX requests - snipped
if (!validate('password',pass_new)) {
$('#pass_new').addClass('error');
$('#pass_confirm_new').addClass('error');
$(error_string.format('Please enter a valid password.')).insertAfter('#pass_confirm_new');
$('#pass_text_short').hide();
$('#pass_text_long').show();
return false;
}
return true;
}
我尚未編輯此代碼以包含已發布的更新代碼,但我的問題是我如何從中return false
以停止表單提交?
function validate(request_type, request_text, callback) {
$.post("http://www.example.com/ajax/validate.php",{
type: request_type,
text: request_text
}, function(data) {
callback(data == "valid");
});
}
用法是:
validate(request_type, request_text, function (isValid) {
if(isValid) {
// do something
} else {
// do something if invalid
}
});
除非您進行同步AJAX 調用(您可能不想這樣做),否則您根本做不到。
如果此 function 在您的代碼中的多個位置使用,您最好的選擇可能是讓它接收function。
這樣,您無需依賴從 function 返回的結果在某些代碼中使用,而是直接將代碼傳入,因此可以確保能夠使用響應。
var my_form = $('#my_form');
my_form.submit( valid_pass_sett );
function valid_pass_sett() {
//code to remove errors left over from previous submissions - snipped
pass_old = $('input[name=pass_old]').val();
pass_new = $('input[name=pass_new]').val();
pass_confirm_new = $('input[name=pass_confirm_new]').val();
validate('password', pass_new, pswd_validation_callback); // async validation
return false; // cancel form submission
}
function validate(request_type, request_text, callback ) {
$.post("http://www.example.com/ajax/validate.php",{
type: request_type,
text: request_text
}, callback );
}
function pswd_validation_callback( data ) {
if ( data === 'valid' ) {
// if valid, call the native .submit() instead of the jQuery one
my_form[ 0 ].submit();
} else {
// Otherwise do your thing for invalid passwords.
// The form has already been canceled, so no concerns there.
$('#pass_new').addClass('error');
$('#pass_confirm_new').addClass('error');
$(error_string.format('Please enter a valid password.')).insertAfter('#pass_confirm_new');
$('#pass_text_short').hide();
$('#pass_text_long').show();
}
}
編輯:更改為使用有問題的代碼。
編輯:更新以使用發布的其他代碼。 為了清楚起見,將答案縮小到命名的 function。
function validate(request_type, request_text) {
$.post("http://www.example.com/ajax/validate.php",{
type: request_type,
text: request_text
}, function(data) {
return (data == "valid");
}); }
您不能真正從“驗證”返回 AJAX 調用的結果。 您可以嘗試在 $.post 調用之前聲明一個變量,我們稱其為“x”,並在響應 function 中將值分配給該變量(x=data=="valid"),並在 $.post 塊之外,但在“驗證”function 中,返回該值。
function validate(request_type, request_text) {
var x;
$.post("http://www.example.com/ajax/validate.php",{
type: request_type,
text: request_text
}, function(data) {
x = data == "valid";
});
return x; }
真正的問題是 function 的“驗證”將繼續,即使后調用沒有返回任何值,所以它總是“假”。
您可以做的最好的事情是在響應 function 內部調用另一個 function,這樣您就可以確保在進入下一部分之前服務器調用已經結束。
編輯:
自從我發布這個答案以來已經有很長時間了。 世界已經改變,所以 AJAX 打電話。
現在我們有了承諾;)
You still cannot return a direct value from a function, but you can return a Promise object, which can be chanined to another Promise , and the second promise will get the data you returned from the first one.
function validate(request_type, request_text) {
var promise = $.ajax("http://www.example.com/ajax/validate.php",{
type: request_type,
text: request_text
});
function getStuff(data) {
//Do something and return the data to the next promise
return data;
}
promise.then(getStuff).then(function(data){
// Do something else with data
});
}
如果我正確理解了這個問題,您可以通過簡單地將返回值存儲到 HTML 元素中,然后從您的自定義 function 中返回該元素值來實現您想要的:
function validate(request_type, request_text){
$.post("http://www.example.com/ajax/validate.php",{
type: request_type,
text: request_text},
function(data) {
$('#someElement').text(data);
});
//return the data by getting the value of the html element
return $('#someElement').text();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.