如何使JQuery的-AJAX请求同步 [英] How to make JQuery-AJAX request synchronous

查看:240
本文介绍了如何使JQuery的-AJAX请求同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么做一个Ajax请求的同步?

我有需要提交一个表单。但是它需要仅当用户输入了正确的密码提交。

下面的格式code:

 <表格名称=形行动=insert.php方法=邮报的onsubmit =返回ajaxSubmit会(本); >
 

和jQuery的code发送和确认密码是这样的:

  VAR ajaxSubmit会=功能(formE1){

            VAR密码= $ .trim($('#employee_password)VAL());

             $阿贾克斯({
                键入:POST,
                异步:假的,
                网址:checkpass.php
                数据:密码=+密码,
                成功:函数(HTML){
                    。VAR ARR = $ parseJSON(HTML);
                    如果(ARR ==成功)
                    {返回true;
                    }
                    其他
                    {返回false;
                    }
                }
            });

        }
 

不过的形式总是提交,而不管Ajax请求返回的值。我已经检查一切。 ARR的价值出来是成功时输入正确密码,并且能够正常运行,反之亦然了。

我要如何提出这一要求的同步?据我可以调试,该请求是异步这样的形式被提交的请求被完成之前。

$ C $下checkpass.php

 < PHP
要求(包括/ apptop.php);
要求(类/ class_employee.php);
要求(类/ class_employee_attendance.php);

$ employee_password = $ _ POST ['密码'];

$ M =新员工();
$间 - > setbyid_employee(1);
$ ARR = $ M-> editdisplay_employee();

如果($改编['employee_password'] == $ employee_password)
{
$水库=成功;
}
其他
{
$水库=密码不匹配;
}

回声$水库;
?>
 

更新: 该解决方案已被发现。

正如由奥拉夫Dietshche:ajaxSubmit会的返回值是不是成功的返回值:)功能({...}。 ajaxSubmit会返回没有价值可言,这相当于定义,从而计算结果为真。

这是什么原因,为什么表单总是提交,并独立发送请求同步与否。

所以,我在成功设置一个变量设置为1成功,函数内。并检查它的价值了成功的功能,如果是1成功的功能外,然后我写了返回true,否则返回false。而这工作。

更新的工作code:

  VAR ajaxSubmit会=功能(forme1){

VAR密码= $ .trim($('#employee_password)VAL());

变种测试=0;
      $阿贾克斯({
      键入:POST,
      网址:checkpass.php
  异步:假的,
      数据:密码=+密码,
      成功:函数(HTML)
        {

             如果(HTML ==成功)
                {
                    测试=1;
                }

             其他
                 {
                      警报(密码不正确,请输入正确的密码。);
                      测试=0;
                 }

          }

     });
    如果(测试==1)
    {
    返回true;
    }
    否则,如果(测试==0)
    {
    返回false;
    }
}
 

解决方案

jQuery.ajax()

  

异步布尔
  默认值:true
  默认情况下,所有的请求都异步发送(即此设置为true默认情况下)。如果您需要同步请求,请将此选项设置为false。

因此​​,在您的要求,您必须执行异步:假而不是异步:假的

更新

ajaxSubmit会的返回值的没有的的成功的返回值:函数(){... } ajaxSubmit会返回任何价值可言,这相当于未定义,进而计算结果为真。

的也是这个道理,为什么表单总是提交,并独立发送请求同步与否。

如果您只想提交表单,当响应成功,你必须返回 ajaxSubmit会,然后在成功函数提交表单,因为@halilb已经建议。

这些方针的东西应该工作

 函数ajaxSubmit会(){
    VAR密码= $ .trim($('#employee_password)VAL());
    $阿贾克斯({
        键入:POST,
        网址:checkpass.php
        数据:密码=+密码,
        成功:函数(响应){
            如果(响应==成功)
            {
                $('表')removeAttr('的onsubmit')。 // prevent死循环
                $(形式)提交()。
            }
        }
    });

    返回false;
}
 

How do i make an ajax request synchronous?

I have a form which needs to be submitted. But it needs to be submitted only when the user enters the correct password.

Here is the form code:

<form name="form" action="insert.php" method="post" onSubmit="return ajaxSubmit(this);" >

And the jquery code for sending and checking password is this:

var ajaxSubmit = function(formE1) {

            var password = $.trim($('#employee_password').val());

             $.ajax({
                type: "POST",
                async: "false",
                url: "checkpass.php",
                data: "password="+password,
                success: function(html) {
                    var arr=$.parseJSON(html);
                    if(arr == "Successful")
                    {    return true;
                    }
                    else
                    {    return false;
                    }
                }
            });

        }

However the form always submits, regardless of the value returned by the ajax request. I have checked everything else. The value of arr is coming out to be 'successful' when correct password is entered and works correctly vice versa too.

How do i make this request synchronous? as far as i can debug, the request is asynchronous so the form gets submitted before the request gets completed.

Code for checkpass.php

<?php 
require("includes/apptop.php");
require("classes/class_employee.php");
require("classes/class_employee_attendance.php");

$employee_password=$_POST['password']; 

$m=new employee();
$m->setbyid_employee(1);
$arr=$m->editdisplay_employee();

if($arr['employee_password'] == $employee_password)
{
$res="Successful";  
}
else
{
$res="Password not match";  
}

echo $res;
?>

Update: The solution has been found.

As pointed by Olaf Dietshche: The return value of ajaxSubmit is not the return value of the success: function(){...}. ajaxSubmit returns no value at all, which is equivalent to undefined, which in turn evaluates to true.

And that is the reason, why the form is always submitted and is independent of sending the request synchronous or not.

So, I set a variable to 1 inside success function upon successful. And checked its value out of success function, if it was 1 outside the success function, then i wrote 'return true', else 'return false'. And that worked.

Updated working code:

var ajaxsubmit=function(forme1) {

var password = $.trim($('#employee_password').val());

var test="0";
      $.ajax({
      type: "POST",
      url: "checkpass.php",
  async: false,
      data: "password="+password,
      success: function(html)
        {

             if(html == "Successful")
                {
                    test="1";
                }   

             else
                 {
                      alert("Password incorrect. Please enter correct password.");  
                      test="0";
                 }

          }

     });
    if(test=="1")
    {
    return true;
    }
    else if(test=="0")
    {
    return false;
    }
}

解决方案

From jQuery.ajax()

async Boolean
Default: true
By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false.

So in your request, you must do async: false instead of async: "false".

Update:

The return value of ajaxSubmit is not the return value of the success: function(){...}. ajaxSubmit returns no value at all, which is equivalent to undefined, which in turn evaluates to true.

And that is the reason, why the form is always submitted and is independent of sending the request synchronous or not.

If you want to submit the form only, when the response is "Successful", you must return false from ajaxSubmit and then submit the form in the success function, as @halilb already suggested.

Something along these lines should work

function ajaxSubmit() {
    var password = $.trim($('#employee_password').val());
    $.ajax({
        type: "POST",
        url: "checkpass.php",
        data: "password="+password,
        success: function(response) {
            if(response == "Successful")
            {
                $('form').removeAttr('onsubmit'); // prevent endless loop
                $('form').submit();
            }
        }
    });

    return false;
}

这篇关于如何使JQuery的-AJAX请求同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆