我是 angular-js 的新手,想要提交带有图像的 multipart/form-data 并且它的 $http.post() 方法仅支持 json 格式,所以我想将 formdata 对象转换为 json 格式,
$scope.SubmitForm=function()
{
url = siteurl + '/admin/' + $scope.module + '/add';
var form=document.getElementById("addForm");
var formData=new FormData(form);
$http({
url : url,
method : "POST",
data : formData,
})
.then(function(responseText) {
alert(JSON.stringify(responseText));
//process data
},function(){
alert("hello from error");
});
}
它对我不起作用;我尝试为此制作 json 格式数据并且工作正常
formData={
"first_name" : $('#first_name').val(),
"last_name" : $('#last_name'),
//....
};
但不知道将我的图像文件附加到这种格式;我应该在这里做什么才能找到工作。
有没有办法(函数)将formdata对象转换为json格式
最佳答案
通过在 $http 配置行中添加如下两行代码来解决(感谢大家)-
$http({
url : url,
method : "POST",
data : formData,
transformRequest: angular.identity, // see the angugular js documentation
headers : {'Content-Type':undefined}// setting content type to undefined that change the default content type of the angular js
}).then(function(responseText){
alert(JSON.stringify(responseText));
///$scope.tablerows=$scope.totaltablerows;
///$scope.searchFunction();
},function(){
alert("hello from error");
});
这对我有用
关于javascript - 如何使用 AngularJS 上传 FormData 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33869717/