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

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

WBOY
WBOYOriginal
2016-06-06 20:18:451493browse

<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>

回复内容:

<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>

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

Statement:
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