如何将两个ajax组合在一起

时间:2017-05-18 01:46:50

标签: javascript php html ajax webcam

我有两个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)

    })
    }
}); 
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

希望本文可以提供帮助 http://www.htmlgoodies.com/beyond/javascript/making-promises-with-jquery-deferred.html

你需要收集你的ajax电话作为承诺,并按照下面的代码(从上面的链接改编)得到他们的回应

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

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

答案 1 :(得分:0)

您可以修改.done函数来触发Face检测元素吗?

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

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