如何将两个 ajax 组合在一起

how to combine two ajax together

我有两个 ajax 函数。一种是打开网络摄像头并拍照,另一种 ajax 是上传文件并检测图片中是否有脸。我想将这两个 ajax 结合起来,以便我可以从网络摄像头拍摄一张照片,并将该照片用于面部检测功能。我应该如何结合这两个 ajax?

// Upload image to sever
 document.getElementById("upload").addEventListener("click", function(){
            var dataUrl = canvas.toDataURL("image/jpeg", 0.85);
            $("#uploading").show();
            $.ajax({
                type: "POST",
                url: "html5-webcam-save.php",
                // url: "form-post.php",
                data: {
                    imgBase64: dataUrl,
                    user: "Joe",
                userid: 25
        }
        }).done(function(msg) {
                console.log("saved");
                $("#uploading").hide();
                $("#uploaded").show();
            });
        });
    }, false);
    
    //Face detection
    <form method="post" enctype="multipart/form-data" action="form-post.php"> 
    <input type="file" id="imageFile" name="file"><button type="button" id="testDetect">Submit</button>
</form>

<script>
$("#testDetect").click(function () {
    var file = $('#imageFile')[0].files[0]; 
    var reader  = new FileReader();
    reader.readAsDataURL(file);
    reader.onloadend = function () {
      var imageData = parseImageData(reader.result);
      var data = {};
      data.image = imageData;
      $.ajax({
      url      : "http://localhost/Karios/simple-detect/form-post.php",
      type     : "POST",
      data     :  data,
      dataType : 'text'
    }).done(function(response) {
      alert(response)
      console.log(response)

    })
    }
}); 

能否修改 .done 函数以触发人脸检测元素?

.done(function(msg) {
    console.log("saved");
    $("#uploading").hide();
    $("#uploaded").show();

    // Trigger the click event
    $('#testDetect').click();
});

希望本文对您有所帮助 http://www.htmlgoodies.com/beyond/javascript/making-promises-with-jquery-deferred.html

您需要按照承诺收集您的 ajax 电话,并按照下面的代码(改编自上面的 link)收集他们的响应

var xhr1 = $.ajax("/some1");
var xhr2 = $.ajax("/some2");

$.when(xhr1, xhr2).done(function(x1, x2) {
  //Handle
});