【问题标题】:transformRequest: angular.identity not working in angular-4transformRequest:angular.identity 在 angular-4 中不起作用
【发布时间】:2018-02-26 01:08:04
【问题描述】:

我正在尝试使用 Form-data 发送文件,但是这个脚本在 angular-4 中不起作用,虽然它在 angular-2 中起作用

var fd = new FormData();
var datas = $("#fileForm").serializeArray();
// send other data in the form
for( var i = 0; i < datas.length; i++ ) {
    fd.append(datas[i].name, datas[i].value);
};
// append file to FormData
fd.append("file", $("#fileInput")[0].files[0])
// for sending manual values
fd.append("email", this.email+'');
fd.append("category", this.cate+'');
fd.append("pricepackage", this.pkg+'');
fd.append("duration", this.pkg_d+'');
fd.append("secret_key", this.srt_key+'');

var url = "http://127.0.0.1:8000/func/textfile_sent/";
this.http_obj.post(url,fd, {
    headers: {'Content-Type': undefined},
    transformRequest : angular.identity
}).success(function(data, status, headers, config) {
  // this callback will be called asynchronously
  // when the response is available
}).
error(function(data, status, headers, config) {
  // called asynchronously if an error occurs
  // or server returns response with an error status.
});

transformRequest : angular.identity 处显示错误,错误为:

TS2345:类型参数 '{ headers: { 'Content-Type': undefined; };转换请求:任何; }' 不可分配给“RequestOptionsArgs”类型的参数。对象字面量只能指定已知属性,而“RequestOptionsArgs”类型中不存在“transformRequest”

【问题讨论】:

    标签: angular typescript file-upload http-headers multipartform-data


    【解决方案1】:

    目前尚不清楚这在哪个版本的 Angular 中发生了变化(如果有的话),但在当前版本的 Angular 中,RequestOptionsArgs 仅具有以下属性:

    正文、标头、方法、参数、responseType、搜索、url、withCredentials。

    所以只需将您的代码更改为:

    //...
    this.http_obj.post(url,fd, {
        headers: {'Content-Type': undefined}, //remove the extraneous property
    }).success(function(data, status, headers, config)
    

    【讨论】:

      猜你喜欢
      • 2018-10-19
      • 2018-08-21
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      相关资源
      最近更新 更多