$。员额()没有足够的时间来运行? [英] $.post() doesn't have time to run?

查看:132
本文介绍了$。员额()没有足够的时间来运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从表单之前,将数据发送到外部脚本来提交表单,但我似乎无法获得数据到达外部脚本,除非我返回false; 的形式本身。

  $(文件)。就绪(函数(){

  //处理表单提交
  $(#的MainForm)。递交(函数(){

    //重置错误阵
    错误=新的Array();

    / *验证code删除 - 不相关* /

    //检查是否存在错误
    如果(errors.length大于0){
      返回false;
    } 其他 {
      $(div.errors)HTML()。
      $。员额(post.php中,{
        名:name_first.val(),
        姓氏:name_last.val()
        家国:home_state.val()
        主要通过电子邮件将:email_primary.val()}
      );
    }
    返回false; / *删除此行,和$。员额将无法正常工作。 * /
  });
});
 

解决方案

我今天遇到了相同问题。像马克说,这是因为Ajax调用是异步的。最简单的解决方法是,使之同步。

使用.ajaxSetup()之前,像这样的任何Ajax调用:

  $ ajaxSetup({异步:假})。
 

I'm trying to send data from a form to an external script prior to submitting the form, yet I cannot seem to get the data to reach the external script unless I return false; on the form itself.

$(document).ready(function(){   	

  // Handle Form-Submission
  $("#mainForm").submit(function(){  

    // Reset Error Array
    errors = new Array();

    /* Validation Code Removed - Not Relevant */    	

    // Check if errors exist
    if (errors.length > 0) {
      return false;
    } else {
      $("div.errors").html("");
      $.post("post.php",{
        "First Name":name_first.val(),
        "Last Name":name_last.val(),
        "Home State":home_state.val(),
        "Primary Email":email_primary.val()}
      );
    }
    return false; /* Remove this line, and the $.post won't work. */
  });
});

解决方案

I ran into the exact same problem today. Like Marc says, it's because the ajax call is asynchronous. The simplest fix is to make it synchronous.

Use .ajaxSetup() before any ajax calls like such:

$.ajaxSetup({async: false});

这篇关于$。员额()没有足够的时间来运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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