I’m trying to send Ajax POST using Struts action form.
I’ve succeeded to create this kind of call using jQuery.
ActionForm
:
public class AjaxForm extends ActionForm { private static final long serialVersionUID = 7403728678369985647L; private String name = null; private FormFile uploadedFile = null; public FormFile getuploadedFile() { return uploadedFile; } public void setFile(FormFile uploadedFile) { this.uploadedFile = uploadedFile; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Action
:
public class AjaxAction extends Action{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { AjaxForm ajaxForm = (AjaxForm)form; System.out.println("Hello " + ajaxForm.getName());
jQuery Ajax POST (working):
<script src="/BusinessProcess_Project/jquery.js"></script> <script type="text/javascript"> function doAjaxPost() { // get the form values //var name = $('#name').val(); var formData ={ 'name':'jQuery_Oron' }; $.ajax({ type: "POST", url: "/BusinessProcess_Project/AjaxSubmit.do", // data: "name=" + 'jQuery', data:formData, dataType: "text/json", success: function(response){ }, error: function(e){ alert('Error: ' + e); } }); } </script>
AngularJS Ajax POST (not working):
<script type="text/javascript"> $scope.myShabi = {}; $scope.myShabi.doClick = function(item, event) { // $scope.init = function(item, event) { $scope.loading = true; var formData ={ 'name':'Angular_Oron' }; var responsePromise = $http({ method: 'POST', url: '/BusinessProcess_Project/AjaxSubmit.do', data: formData }); responsePromise.success(function(data, status, headers, config) { $scope.jsonFromServer = data; $scope.status ="Finished"; $scope.loading = false; }); responsePromise.error(function(data, status, headers, config) { alert("AJAX failed!"); }); }; </script>
for some reason AngularJS doesn’t like the way I’m sending the ActionForm
data.
Advertisement
Answer
It is not data
that you should use, but params
:
... var responsePromise = $http({ method: 'POST', url: '/BusinessProcess_Project/AjaxSubmit.do', // data: formData params: formData }); ...