且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

设置Content-Type:在角度js post请求中

更新时间:2021-11-23 21:39:18

$ http.post angular中的快捷方法需要三个参数,url,请求数据和一个配置对象,您可以在其中设置如下所示的标题:

$http.post shortcut method in angular takes three parameters, url, request data and a config object in which you can set headers like below :

$http.post('/someUrl', data, {headers:{'Content-Type': 'multipart/form-data'}}).then(successCallback, errorCallback);

在您的情况下,它将是:

In your case it will be :

$http.post($scope.base_url+'create.php', 
        {params {view:'view',articleimg:$scope.articleimg}}, {headers:{'Content-Type': 'multipart/form-data'})
        .then(function(response){
                console.log(response);
});

您还可以构建如下所示的请求对象:

You can also construct the request object like below :

{
 method: 'POST',
 url: $scope.base_url+'create.php',
 headers: {
   'Content-Type': 'multipart/form-data'
 },
 data: {params {view:'view',articleimg:$scope.articleimg}}
}

然后按以下方式发出请求:

and then make the request like this :

$http(req).then(function(){...}, function(){...});

如果要设置常见的应用程序范围标题,可以使用将添加到所有标题的默认标题默认情况下请求如下:

If you want to set common application wide headers , you can use default headers which will be added to all the requests by default like below :

$httpProvider.defaults.headers.common['Content-Type'] = 'multipart/form-data';

这将为所有请求添加上述内容类型。

This will add the above mentioned content type for all the requests.

文档中的更多信息这里