设置Content-Type:在角度js post请求中 [英] setting Content-Type: in angular js post request

查看:1152
本文介绍了设置Content-Type:在角度js post请求中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<input type="file" ng-model="articleimg" placeholder="upload related img">    

$http.post($scope.base_url+'create.php', 
            {params {view:'view',articleimg:$scope.articleimg}})
            .then(function(response){
                    console.log(response);
    });

我想知道如何以及在何处指定
内容类型:multipart /此angularjs发布请求中的表单数据?

I would like to know how and where to specify the content-type: multipart/form-data in this angularjs post request?

请协助。
默认似乎是application / json,text / plain,
,它不适用于图像/文件上传。

Please assist. the default seems to be "application/json, text/plain," which does not work with the image/file uploads.

if(isset($_FILES['articleimg'])===true ){
  echo "sucessfull";
}else echo "unsuccessfull";

上面的代码alway echos unsuccessfull。

the code above alway echos unsuccessfull.

推荐答案

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

文档中的更多信息这里

这篇关于设置Content-Type:在角度js post请求中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆