简体   繁体   中英

Adding content-type header to Angular $http request

I'm trying to send HTTP request using the following code:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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