本文介绍了设置标题和HTTP PARAMS在restangular单个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用restangular进行文件上传后的要求,我想实现相同的功能下面restangular。
不过,我不确定如何设置内容类型和transformRequest只是这个特殊的请求。如果我理解正确,setDefaultHeader将其设置为所有后续请求。是否有一些其他的方式?

  myApp.service('$文件上传',['$ HTTP',函数($ HTTP){
   this.uploadFileToUrl =功能(文件的uploadURL){
      VAR FILEDATA =新FORMDATA();
      filedata.append(文件,文件);
      $ http.post(的uploadURL,FILEDATA,{
         transformRequest:angular.identity,
         标题:{'内容类型':未定义}
      })
      .success(函数(){
      })
      .error(函数(){
      });
     }
}]);


解决方案

您有2个的情况下在这里,POST用于创建一个新的项目或PUT编辑项目:

  //保存新项目
$ scope.saveNew =功能(项目){  VAR数据=新FORMDATA();
  angular.forEach(项目,功能(fieldData,场){
    data.append(场,fieldData);
  });  Restangular
    。所有项目')
    .withHttpConfig({transformRequest:angular.identity})
    。员额(数据,{},{'的Content-Type:未定义})
    。然后(函数(){
      //做成功
    },函数(){
      //做失败
    });
};//编辑现有项目
$ scope.save =功能(项目){  VAR数据=新FORMDATA();
  angular.forEach(item.plain()函数(fieldData,场){
    data.append(场,fieldData);
  });  Restangular
    。一('项目',item._id)
    .withHttpConfig({transformRequest:angular.identity})
    .customPUT(数据,不确定,{},{'的Content-Type:未定义})
    。然后(函数(){
      $ location.path('网站');
    });

I am trying to use restangular for file upload post request , I want to achieve the same functionality as below in restangular.However, I was unsure how to set content type and transformRequest for just this particular request. If I understand correctly, setDefaultHeader sets it for all subsequent requests. Is there some other way?

myApp.service('$fileUpload', ['$http', function ($http) {
   this.uploadFileToUrl = function(file, uploadUrl){
      var filedata = new FormData();
      filedata.append('file', file);
      $http.post(uploadUrl, filedata, {
         transformRequest: angular.identity,
         headers: {'Content-Type': undefined}
      })
      .success(function(){
      })
      .error(function(){
      });
     }
}]);
解决方案

You have 2 situations here, the POST for create a new item or the PUT to edit an item:

// Save new Item
$scope.saveNew = function (item) {

  var data = new FormData();
  angular.forEach(item, function (fieldData, field) {
    data.append(field, fieldData);
  });

  Restangular
    .all('items')
    .withHttpConfig({transformRequest: angular.identity})
    .post(data, {}, {'Content-Type': undefined})
    .then(function () {
      // do on success
    }, function () {
      // do on failure
    });
};

// Edit existing Item
$scope.save = function (item) {

  var data = new FormData();
  angular.forEach(item.plain(), function (fieldData, field) {
    data.append(field, fieldData);
  });

  Restangular
    .one('items', item._id)
    .withHttpConfig({transformRequest: angular.identity})
    .customPUT(data, undefined, {}, {'Content-Type': undefined})
    .then(function () {
      $location.path('sites');
    });

这篇关于设置标题和HTTP PARAMS在restangular单个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 07:43