php 如何返回数据响应ajax的真假函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19345302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How return true or false function of data response ajax?
提问by BlackWhite
I want return true if data is empty or false if data is not empty.If data is empty continue with form action else stay in current page.
如果数据为空,我想返回真,如果数据不为空,我想返回假。如果数据为空,继续表单操作,否则留在当前页面。
Code is:
代码是:
<script type="text/javascript">
function error()
{
var response = false;
$.ajax({
url: BASE_URL + 'auth/validateForm',
type: 'POST',
data: {
surname: $("#r-surname").val()
},
dataType: "json",
success: function(data) {
if(data.surname)
{
$('#error_surname').html(data.surname);
response = false;
}else{
$('#error_surname').html('');
response = true;
}
},
error: function(data) {
return false;
}
});
alert(response);
return response;
}
</script>
<form action="{$BASE_URL}contul-meu" method="post" name="registration" onsubmit="return error()">
<table>
<tr>
<td>
<label for="r-surname">Prenume*</label>
</td>
<td>
<input type="text" class="text" id="r-surname" name="surname" value="{$user['surname']}"/>
</td>
<td><small id="error_surname" class="err"></small></td>
</tr>
</table>
</form>
php:
php:
public function validateForm()
{
$surname = $this->input->post('surname');
$data = array();
if(strip_tags($surname) == '')
{
$data['surname'] = 'Prenumele este invalid!';
}
echo json_encode($data);
}
var response is only false.How make response true if no error?
var 响应只是假的。如果没有错误,如何使响应为真?
回答by Bas Kuis
You can't return the result of a nested ajax function. The javascript will execute and keep going - however the delay in making the request will happen after the function returns.
您不能返回嵌套 ajax 函数的结果。javascript 将执行并继续运行 - 但是在函数返回后会延迟发出请求。
The best thing is to chain your events. Just create another function which accepts the true or false value, and wrap your logic in there.
最好的办法是链接你的事件。只需创建另一个接受 true 或 false 值的函数,并将您的逻辑包装在那里。
<script type="text/javascript">
function error()
{
var response = false;
$.ajax({
url: BASE_URL + 'auth/validateForm',
type: 'POST',
data: {
surname: $("#r-surname").val()
},
dataType: "json",
success: function(data) {
if(data.surname)
{
$('#error_surname').html(data.surname);
response = false;
doSomethingBasedOnResponse(false);
}else{
$('#error_surname').html('');
response = true;
doSomethingBasedOnResponse(true);
}
},
error: function(data) {
return false;
doSomethingBasedOnResponse(false);
}
});
}
function doSomethingBasedOnResponse(value){
if(value){
alert('yes');
}else{
alert('no');
}
}
</script>
回答by WebNovice
Add an ID to your form
在表单中添加 ID
<form id="registration"..
$("#registration").submit(function(e){
$.ajax({
url: BASE_URL + 'auth/validateForm',
type: 'POST',
data: {
surname: $("#r-surname").val()
},
dataType: "json",
success: function(data) {
if(data.surname)
{
$('#error_surname').html(data.surname);
//prevent form submission
e.preventDefault();
}else{
$('#error_surname').html('');
//no errors, it will continue with form submission
}
},
error: function(data) {
e.preventDefault();
}
});
});
回答by marko
Actually check if the return is empty or null.
实际上检查返回是空的还是空的。
success: function(data) {
if((data != null || data != "") && data.hasOwnProperty("surname"))
{
$('#error_surname').html(data.surname);
response = false;
}else{
$('#error_surname').html('');
response = true;
}