Javascript 如何在Ajax调用中设置标题?

Javascript 如何在Ajax调用中设置标题?,javascript,ajax,Javascript,Ajax,我正在开发使用Angularjs和Web api作为后端的多语言网站。每当我进行API调用时,我都试图在标题中发送RequestedPlatform和RequestedLanguage 下面是我的Ajax请求调用 $http.post(url,RegistrationData).then(function (response) { var pageList = response.data.ID; toastr.succe

我正在开发使用Angularjs和Web api作为后端的多语言网站。每当我进行API调用时,我都试图在标题中发送
RequestedPlatform
RequestedLanguage

下面是我的Ajax请求调用

$http.post(url,RegistrationData).then(function (response) {
                   var pageList = response.data.ID;
                   toastr.success('', 'Registered Succesfully');
                   $state.go('Registration.OTPVerification', { pageList });
               }, function (error) {
                   toastr.error('', 'Error Occured');
               });
更新代码

var RegistrationData = {
                   FirstName: $scope.user.Fname,
                   LastName: $scope.user.Lname,
                   Password: $scope.user.password,
                   Gender: "Male",
                   DateOfBirth: "2017-04-04",
                   Nationality: $scope.user.selectedGlobe,
                   Mobile_CountryCod: "91",
                   MobileNumber: $scope.user.mobilenumber,
                   EmailId: $scope.user.email,
                   Home_Location: $scope.user.homeLocation,
                   Home_City: $scope.user.homeCity,
                   Home_Neighbourhood: $scope.user.homeNeighbourhood,
                   Home_HouseNumber: $scope.user.housenumber,
                   Home_MainStreet: $scope.user.homemainstreet,
                   Home_SubStreet: $scope.user.homesubstreet,
                   Work_Location: $scope.user.worklocation,
                   Work_City: $scope.user.workcity,
                   Work_Neighbourhood: $scope.user.workNeighbourhood,
                   Work_HouseNumber: $scope.user.workhousenumber,
                   Work_MainStreet: $scope.user.workmainstreet,
                   Work_SubStreet: $scope.user.worksubstreet
               };
               var req = {
                   method: 'POST',
                   url: url,
                   data: { RegistrationData: RegistrationData },
                   headers: {
                       RequestedPlatform: "Web",
                       RequestedLanguage: "English"
                   }
               }
               $http(req).then(function (response) {
                   var pageList = response.data.ID;
                   toastr.success('', 'Registered Succesfully');
                   $state.go('Registration.OTPVerification', { pageList });
               }, function () {
                   toastr.error('', 'Error Occured');
               });

我可以得到一些关于在Ajax中设置标题的帮助吗。任何帮助都将不胜感激。

有几种方法,我已经发布了一个我已经使用了一段时间的方法。我希望你正在寻找下面的内容

$http.post('test', data, {
        withCredentials : false,
        transformRequest : angular.identity,
        headers : {
            'Content-Type' : undefined
        }
})

有几个方法,我已经发布了一个,我已经使用了一段时间。我希望你正在寻找下面的内容

$http.post('test', data, {
        withCredentials : false,
        transformRequest : angular.identity,
        headers : {
            'Content-Type' : undefined
        }
})

您可以使用$http的
headers
属性发送头

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

$http(req).then(function(){...}, function(){...});
如果您想要通过访问
$httpProvider.defaults.headers
配置对象来完全配置所有请求的标题


您可以使用$http的
头属性发送头

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

$http(req).then(function(){...}, function(){...});
如果您想要通过访问
$httpProvider.defaults.headers
配置对象来完全配置所有请求的标题


谢谢。我更新了我的代码。我可以提出请求,但数据未提交。有什么想法吗?如果您发送的是自定义头,那么您需要在服务器端配置以允许和接受感谢。我更新了我的代码。我可以提出请求,但数据未提交。知道吗?如果您要发送自定义头,那么需要在服务器端配置以允许和接受