简体   繁体   English

向Angular $ http请求添加内容类型标头

[英]Adding content-type header to Angular $http request

I'm trying to send HTTP request using the following code: 我正在尝试使用以下代码发送HTTP请求:

var editCompanyUrl = 'http://X.X.X.X:YYYY/editCompany';
var userId = localStorage.getItem("UserId");
var token = localStorage.getItem("Token");
var companyId = localStorage.getItem("companyId");

return $http({
method: 'POST',
url: editCompanyUrl,
params: {
   token: token,
   userId: userId,
   companyId: companyId,
   companyName: $scope.companyName,
   },
   timeout: 500
   }).then(function (data) {
            console.log(data);
            //Store Company ID which is used for saving purposes
            //localStorage.setItem("companyId", data.data.Company.id);
            return data.data.Company;
        }, function (data) {
            console.log(data);
        })

and handler of the request on the server side accepts requests with Content-Type: multipart/form-data. 服务器端的请求处理程序将接受Content-Type:multipart / form-data的请求。 How can I add this content type to the request? 如何将这种内容类型添加到请求中? I've tried many advices and tips from tutorials but no success. 我从教程中尝试了许多建议和技巧,但没有成功。 Could you please help me? 请你帮助我好吗? In addition to it - what should I do when I will add a file with an image to this request? 除此之外-将带有图像的文件添加到此请求时该怎么办? Can I just add it as additional parameter of the request? 我可以将其添加为请求的附加参数吗?

Thank you very much! 非常感谢你!

Angular POST must be like below code. Angular POST必须类似于以下代码。

var req = {
     method: 'POST',
     url: 'http://example.com',
     headers: {
           'Content-Type': undefined
          },
     data: { test: 'test' }
}

it should have data:{ } 它应该具有数据:{}
so try to put your params: inside the data and it should work. 因此,请尝试将您的参数置入数据内,它应该可以工作。

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

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