I recently started using angular-js and encountered a problem when trying to submit multipart/form-data with an image. The $http.post() method only supports json format, so I needed to convert the formdata object to json format.
$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");
});
}
Unfortunately, this approach did not work for me. I then tried converting the data to json format and it worked fine.
formData={
"first_name" : $('#first_name').val(),
"last_name" : $('#last_name'),
//....
};
However, I was unsure of how to append my image file to this format. Any suggestions on how to achieve this?
Is there a specific function that can help in converting formdata object to json format?