简体   繁体   English

Angular HttpPost方法返回为405方法,不允许

[英]Angular HttpPost method returns as 405 method not allowed

Angular HttpPost method returns as 405 method not allowed. Angular HttpPost方法返回不允许的405方法。

service call: 服务电话:

private fileUploadUrl = 'file-tranfer/uploadFile';

formHtppOptions(params): any {
    const httpOptions = {
      headers: {'Content-Type': 'application/json', 'Application-Token': this.getToken()},
      params: params,
             };
    return httpOptions;
  }

getBaseUrl(): string {
     return this.sharedSessionService.sharingData.config.uiService.url;
  }

  getToken(): string {
    return this.sharedSessionService.sharingData.config.uiService.token;
  }

  postFileTransferUpload(formData):  Observable<object> {
    const baseUrl = this.getBaseUrl();
    return this.http.post<Object>(baseUrl + this.fileUploadUrl, formData, this.formHtppOptions({}));
  }

Controller: 控制器:

uploadFile() {
  const formData = new FormData();
  formData.set('file', this.fileToUpload);
   formData.set('company', this.selectedCompany);
  formData.set('fileId', this.selectedFileType);

   this.iportalUploadService.postFileTransferUpload(formData)
  .subscribe(data => {
    debugger;
  });
}

error : console error on upload 错误: 上载控制台错误

Code erroneously sets Content-Type: 'application/json when the data is new FormData() . 当数据是new FormData()时,代码错误地设置了Content-Type: 'application/json

You don't need Content-Type at all: 您根本不需要Content-Type

formHtppOptions(params): any {
    const httpOptions = {
      headers: { ̶'̶C̶o̶n̶t̶e̶n̶t̶-̶T̶y̶p̶e̶'̶:̶ ̶'̶a̶p̶p̶l̶i̶c̶a̶t̶i̶o̶n̶/̶j̶s̶o̶n̶'̶,̶
                 'Application-Token': this.getToken()
      },
      params: params,
    };
    return httpOptions;
}

When the XHR.send method is invoked with a FormData object as data, it automatically sets the content type to "multipart/form-data" and appends the proper part boundary. 当以FormData对象作为数据调用XHR.send方法时 ,它会自动将内容类型设置为"multipart/form-data"并附加适当的零件边界。

Looks like, server to which you are sending the Post request(your Site's server) has been configured to block Post request. 看起来,您要发送发布请求的服务器(您站点的服务器)已配置为阻止发布请求。 Please configure your server to allow the Post request. 请配置您的服务器以允许发布请求。 Its back-end issue I guess 我猜它的后端问题

Please follow below documentation 请遵循以下文档

http://www.checkupdown.com/status/E405.html http://www.checkupdown.com/status/E405.html

As @georgeawg mentioned, you don't need to assign the content-type here, as its unnecessary because the data here is FormData(). 如@georgeawg所述,您无需在此处分配content-type,因为它的不必要是因为这里的数据是FormData()。 So all you need to do is to remove this part 所以您要做的就是删除这部分

'Content-Type': 'application/json' '内容类型':'应用程序/ json'

from the headers. 从标题开始。 Also make sure that you include Access-Control-Allow-Methods: POST in your headers if possible (only if the above mentioned solution doesnot works fine) as it might be the case that your POST request gets blocked on the browser side as its not allowed. 还要确保您在标头中包括Access-Control-Allow-Methods:POST(仅当上述解决方案无法正常工作时),因为在您的POST请求可能会在浏览器端被阻止的情况下,这种情况可能会发生。允许。

Hope it works totally fine now ! 希望现在一切正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM