Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.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 Js+;如何使用blob将数据参数发送到servlet_Javascript_Ajax_Servlets - Fatal编程技术网

Javascript Js+;如何使用blob将数据参数发送到servlet

Javascript Js+;如何使用blob将数据参数发送到servlet,javascript,ajax,servlets,Javascript,Ajax,Servlets,我有以下js代码将音频文件发送到servlet。我需要向servlet发送另外两个参数。以下是我目前的代码 form = new FormData(), request = new XMLHttpRequest(); form.append("file",blob, filename); request.open( "POST", "AudioToText", true ); request.send(form); 有人能帮我写js代码吗

我有以下js代码将音频文件发送到servlet。我需要向servlet发送另外两个参数。以下是我目前的代码

form = new FormData(),
request = new XMLHttpRequest();
form.append("file",blob, filename);
request.open(
        "POST",
        "AudioToText",
        true
    );
request.send(form);

有人能帮我写js代码吗?如何在这个请求中添加两个额外的参数,比如id/pass,以及如何在servlet中获取它们。提前感谢。

request.open()中的第二个参数假设指向将在绝对链接中处理数据的文件,还有另一个名为onreadystatechanged的XMLHttpRequest类型的属性,它需要一个回调来检查请求是否成功

示例代码-忽略带有self的行,这些只是指向方法/属性的变量,不依赖于执行请求:

        // if an Object to connect to server was created begin transfer
        if (this.xhr) {

            var processData = function () {
                // if everything is ok carry on
                if (self.xhr.readyState === 4 && self.xhr.status === 200) {
                    if(self.responseType === "text"){
                        self.Success(self.xhr.responseText);
                    }else{
                        self.Success(self.xhr.responseXML);
                    }
                }
                // if something fails along the way else execute failure method
                else if (self.xhr.status !== 200 && self.xhr.readyState === 4) {
                // something went wrong
                }
            };


            // open connection
            this.xhr.open(this.method, this.host, true);


            // set up the connection to a method to process the data
            //noinspection JSPrimitiveTypeWrapperUsage
            this.xhr.onreadystatechange = processData;

            // send the data
            this.xhr.send(this.postData);

我的问题有点不同。我想在一个servlet调用中发送一个文件(blob)和两个参数(userid和password)。因此,文件、用户ID和密码必须在一次调用中从javascript发送到servlet,然后将用户ID和密码附加到ajax调用中。如果用户登录,您可以将其用户ID存储在cookie中,让javascript获取并将其添加到ajax调用中作为参数(将在FormData对象中)。。。如果您的每个用户都有一个唯一的ID,您就不需要发送他们的密码,因为您可以根据他们的ID在服务器端查询密码