ats*_*ann 1 javascript post content-type http angularjs
<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);
});
Run Code Online (Sandbox Code Playgroud)
我想知道如何以及在何处指定此 angularjs 发布请求中的内容类型:multipart/form-data?
请协助。默认似乎是“application/json, text/plain”,它不适用于图像/文件上传。
if(isset($_FILES['articleimg'])===true ){
echo "sucessfull";
}else echo "unsuccessfull";
Run Code Online (Sandbox Code Playgroud)
上面的代码总是回显不成功。
angular 中的 $http.post快捷方法采用三个参数,url、请求数据和一个配置对象,您可以在其中设置标题,如下所示:
$http.post('/someUrl', data, {headers:{'Content-Type': 'multipart/form-data'}}).then(successCallback, errorCallback);
Run Code Online (Sandbox Code Playgroud)
在您的情况下,它将是:
$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);
});
Run Code Online (Sandbox Code Playgroud)
您还可以像下面这样构造请求对象:
{
method: 'POST',
url: $scope.base_url+'create.php',
headers: {
'Content-Type': 'multipart/form-data'
},
data: {params {view:'view',articleimg:$scope.articleimg}}
}
Run Code Online (Sandbox Code Playgroud)
然后像这样提出请求:
$http(req).then(function(){...}, function(){...});
Run Code Online (Sandbox Code Playgroud)
如果要设置通用的应用程序范围的标头,可以使用默认标头,默认情况下,这些标头将添加到所有请求中,如下所示:
$httpProvider.defaults.headers.common['Content-Type'] = 'multipart/form-data';
Run Code Online (Sandbox Code Playgroud)
这将为所有请求添加上述内容类型。
此处的文档中的更多信息
| 归档时间: |
|
| 查看次数: |
18468 次 |
| 最近记录: |