Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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
如何在Ajax回调中调用Jquery函数_Jquery_Jquery Plugins_Getusermedia - Fatal编程技术网

如何在Ajax回调中调用Jquery函数

如何在Ajax回调中调用Jquery函数,jquery,jquery-plugins,getusermedia,Jquery,Jquery Plugins,Getusermedia,我有两个不同的文件,我正在调用jquery函数 它是在我的File2.js中从file1.php创建的 #file1.php 在我的第一个文件中,我调用了ajax中的Jquery函数 <!DOCTYPE html> <html> <head> <title></title> </head> <body> <video id="video" autoplay></vid

我有两个不同的文件,我正在调用jquery函数

它是在我的File2.js中从file1.php创建的

#file1.php 在我的第一个文件中,我调用了ajax中的Jquery函数

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<video id="video" autoplay></video>
<button id="start">Start</button>
<script type="text/javascript">
$(document).ready(function(){
    getusermedia();
    function getusermedia(media){
        $.ajax({
            url:"getusermedia.js",
            method:"POST",
            data:{media},
            success:function(data){
                $("#video").html(data);
            }
        })
    }
})
</script>
</body>
</html>
我得到答案的时候

我把这两个代码放在一个文件中


但是在两个不同的文件中,它没有响应

ajax调用找不到html元素
const localVideo=document.querySelector('video')因为它不可见


您应该在php文件类型中调用html视频标记。这是一个选项,否则只会出现错误。

如果要执行通过AJAX检索的JavaScript脚本,请使用
$.getScript()


您没有将参数传递给
getusermedia()
,JS脚本也没有使用任何参数,因此我已从您的函数中删除
media

您没有导入file2.JS,也不会发布到getusermedia.JS工作将帖子发送到
。JS
文件不会执行JavaScript。您可以使用
$.getScript()
使用AJAX加载
.js
文件。但是脚本应该如何使用
media
参数呢?
const mediaStreamConstraints = {
  video: true,
  audio: true
};
const localVideo = document.querySelector('video');
let localStream;
function gotLocalMediaStream(mediaStream) {
  localStream = mediaStream;
  localVideo.srcObject = mediaStream;
}
function handleLocalMediaStreamError(error) {
  console.log('navigator.getUserMedia error: ', error);
}
navigator.mediaDevices.getUserMedia(mediaStreamConstraints)
  .then(gotLocalMediaStream).catch(handleLocalMediaStreamError);
function getusermedia() {
  $.getScript("getusermedia.js");
}