Getting the result of jquery .post() function
I need to find out how to access "data" variable outside of the post function. It will return either valid
or invalid
so I can finish the main function logic.
Is this the right way to do it:
$('#form_choose_methods'开发者_如何学Go).submit(function(){
var voucher_code = $('#voucher_code').val();
var check = $.post(baseURL+"ajax.php", { tool: "vouchers", action: "check_voucher", voucher_code: voucher_code },
function(data) {
});
alert(check);
return false;
});
check
seems to be the object, but I want to know how to access result of it.
You can access the response in the success callback you use
$.post(baseURL+"ajax.php", { tool: "vouchers", action: "check_voucher", voucher_code: voucher_code },
function(data) {
// you can access the response in here
alert(data);
});
Ajax calls are asynchronous, so you will only have access to the result from the callback whenever it completes..
$('#form_choose_methods').submit(function () {
var voucher_code = $('#voucher_code').val();
$.post(baseURL + "ajax.php", { tool: "vouchers", action: "check_voucher", voucher_code: voucher_code },
function (data) {
if (data == "valid") {
//do seomthing
}
else {
//do something else
}
});
});
精彩评论