Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我需要帮助找到同步jQuery ajax的替代方案_Javascript_Jquery_Logic - Fatal编程技术网

Javascript 我需要帮助找到同步jQuery ajax的替代方案

Javascript 我需要帮助找到同步jQuery ajax的替代方案,javascript,jquery,logic,Javascript,Jquery,Logic,我有一个非常复杂的表单,它包含多个选项卡。每个选项卡都包含一个唯一的实例(用于上载多个图像)。该表单允许用户上传医学图像“病例”,其中每个病例由多个成像“研究”(例如CT扫描)组成,每个研究包含多个图像 当用户单击“提交”按钮时,我会使用jQuery截获单击,因为我需要执行以下操作: 检查是否输入了所需字段[简单] 从我的服务器获取唯一的id号。每个Plupload实例都需要此id号才能知道要上载到哪个目录 在表单提交时调用的函数中,我有以下代码段: var case_id; // Code

我有一个非常复杂的表单,它包含多个选项卡。每个选项卡都包含一个唯一的实例(用于上载多个图像)。该表单允许用户上传医学图像“病例”,其中每个病例由多个成像“研究”(例如CT扫描)组成,每个研究包含多个图像

当用户单击“提交”按钮时,我会使用jQuery截获单击,因为我需要执行以下操作:

  • 检查是否输入了所需字段[简单]
  • 从我的服务器获取唯一的id号。每个Plupload实例都需要此id号才能知道要上载到哪个目录
  • 在表单提交时调用的函数中,我有以下代码段:

    var case_id;
    
    // Code to check the required fields are entered
    ....
    
    // Get the case id number from the server
    $.get('ajax/unique-case-id').done(function(data){
        case_id = data;
    });
    
    // do something with case_id and other things. MUST happen after the ajax call
    ....
    
    // if there was a problem uploading the images, stop the form from submitting
    if (problem_occured) {
        return false;
    }
    
    根据我当前的逻辑,我需要脚本暂停,直到它获得case_id。这在jQuery 1.8之前是可能的,但是$.ajax()async:false属性已被弃用

    我的问题有两个方面:

  • 有没有办法在我获得所需的case\u id之前保留脚本
  • 如果没有,你知道我该如何改变我的逻辑来解决这个问题吗

  • 你可能想知道为什么案例id如此重要。plupload实例在表单提交之前进行上传,它们需要上传到一个目录。我希望上传的图像进入我的服务器上一个名为case_id的文件夹。这将让服务器上的PHP脚本在获得表单发布的其余数据后确定如何处理这些图像。

    这是一个非常常见的“问题”,可以通过适当地使用回调来轻松解决

    $("#submitButton").click(function (event) {
        event.preventDefault(); //Don't submit the form, we'll submit it manually.
    
        var case_id;
    
        // Code to check the required fields are entered
        ....
    
        // Get the case id number from the server
        $.get('ajax/unique-case-id').done(function(data){
            case_id = data;
    
            // do something with case_id and other things. MUST happen after the ajax call
            ....
    
            // if there was a problem uploading the images, stop the form from submitting
            if (problem_occured) {
                alert("something went wrong");
            } else {
                $("#referenceToTheForm").submit();
            }
    
        });
    });
    

    长话短说,将回调中的“处理问题或提交表单”保留到$.get调用实际上会导致脚本“暂停”,直到它返回数据。然后,您可以使用类似spin.js的工具为用户提供良好的等待体验,直到完成为止。

    您需要使所有内容都异步,然后重新提交表单。这并不重要,但您可能希望将
    事件.preventDefault()作为处理程序的第一行。这样,如果在主代码中抛出任何错误,它就不会被跳过。谢谢你,Stephen。工作完美。要是我能在5小时前自己弄明白就好了!没问题。。当您尝试等待多个服务响应时,它会变得更加有趣:)