我的$ .post查询有问题。这是我的代码:
function verif_subscribe_sale() {
var URL_ROOT = "http://my_website.com/";
var email_sale = document.getElementById('email-sale');
var name_sale = document.getElementById('name-sale');
var url_sale = document.getElementById('url-sale');
var password_sale = document.getElementById('password-sale');
var token = document.getElementsByName('_token');
test = 0;
$.post(URL_ROOT + 'check-url', { url: url_sale.value, token: token.value } ).done(function( data ) {
if(data == "notok" || data.length < 4){
document.getElementById('error_url').style.display="block";
test = 1;
}
});
if(name_sale.value.length < 4){
document.getElementById('error_name_length').style.display="block";
test = 1;
}
if(check_email(email_sale) != true) {
document.getElementById('error_mail').style.display="block";
test = 1;
}
if(test == 0){
return true;
}else{
return false;
}}
此代码从HTML表单调用:
<form action="{{{ URL::to('create_my_sale') }}}" method="post" onsubmit="return verif_subscribe_sale();">
问题是,由于post是异步的,即使post返回不满足条件,也可以提交表单。 是否可以在提交表单之前等待post循环结束?我尝试了一些stackoverflow解决方案(如回调),但它不起作用......
编辑部分
我在JS中尝试过这种方式:
function verif_subscribe_sale(callback) {
var URL_ROOT = "http://www.mysite.com/";
var url_sale = document.getElementById('url-sale');
var token = document.getElementsByName('_token');
test = 0;
$.post(URL_ROOT + 'check-url', { url: url_sale.value, token: token.value } ).done(function( data ) {
if(data == "notok" || data.length < 4){
document.getElementById('error_url').style.display="block";
test = 1;
}
});
callback(test);
}
function test_callback(test){
var name_sale = document.getElementById('name-sale');
var email_sale = document.getElementById('email-sale');
var password_sale = document.getElementById('password-sale');
if(name_sale.value.length < 4){
document.getElementById('error_name_length').style.display="block";
test = 1;
}
if(check_email(email_sale) != true) {
document.getElementById('error_mail').style.display="block";
test = 1;
}
if(test == 0){
return true;
}else{
return false;
}
}
我称之为:
<form action="{{{ URL::to('create_my_sale') }}}" id="form-sale" method="post" onsubmit="return verif_subscribe_sale(test_callback);">
但它也不起作用......
答案 0 :(得分:0)
您最好使用ajax,并控制您的响应(异步工作): 以下是示例代码:
var tout;
// ...
// some code ...
// ...
function setExpressCheckout() {
'use strict';
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
var s;
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
if (tout) {
clearTimeout(tout);
}
s = xmlhttp.responseText;
// put your own code here
}
};
function ajaxTimeout() {
xmlhttp.abort();
// put some message here, or whatever ...
clearTimeout(tout);
}
clearTimeout(tout);
tout = setTimeout(function () {
ajaxTimeout();
}, 15000); // 15000 miliseconds = 15 seconds. you can choose your own timeout.
xmlhttp.open("POST", "yourPage.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("");
}
祝你好运!!!
答案 1 :(得分:0)
$ .post只是$.ajax的捷径({type:“POST”,...})。
使用$ .ajax,你可以使用参数{async:false}等待任何时间,然后继续执行(超时参数也可用)。