Home > Backend Development > PHP Tutorial > javascript - js上传文件,使用了formdata对象,可若设置了setRequesetHeader,会失败是怎么回事?

javascript - js上传文件,使用了formdata对象,可若设置了setRequesetHeader,会失败是怎么回事?

WBOY
Release: 2016-06-06 20:18:45
Original
1517 people have browsed it

<code>   formdata.append('images',type);
   var xhr=new XMLHttpRequest();
       xhr.open('post',path,true);
     // xhr.setRequestHeader('content-type','multipart/form-data'); 若这儿一旦设置就会上传失败,这是怎么回事?难道是类型设置错误?
       xhr.send(formdata);
       xhr.onreadystatechange=function(){
         if (this.readyState===4 && this.status===200)
          {
            if (this.responseText)
             {
              var data=this.responseText;
              console.log(this.responseText);
             }
          }
       }</code>
Copy after login
Copy after login

回复内容:

<code>   formdata.append('images',type);
   var xhr=new XMLHttpRequest();
       xhr.open('post',path,true);
     // xhr.setRequestHeader('content-type','multipart/form-data'); 若这儿一旦设置就会上传失败,这是怎么回事?难道是类型设置错误?
       xhr.send(formdata);
       xhr.onreadystatechange=function(){
         if (this.readyState===4 && this.status===200)
          {
            if (this.responseText)
             {
              var data=this.responseText;
              console.log(this.responseText);
             }
          }
       }</code>
Copy after login
Copy after login

你使用 send 发送 formdata 的时候不需要在设置 form-data 的头了,设置了反而有问题。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template