I’m trying to send Ajax POST using Struts action form.
I’ve succeeded to create this kind of call using jQuery.
ActionForm
:
JavaScript
x
25
25
1
public class AjaxForm extends ActionForm {
2
private static final long serialVersionUID = 7403728678369985647L;
3
4
private String name = null;
5
private FormFile uploadedFile = null;
6
7
8
public FormFile getuploadedFile() {
9
return uploadedFile;
10
}
11
12
public void setFile(FormFile uploadedFile) {
13
this.uploadedFile = uploadedFile;
14
}
15
16
17
public String getName() {
18
return name;
19
}
20
21
public void setName(String name) {
22
this.name = name;
23
}
24
}
25
Action
:
JavaScript
1
9
1
public class AjaxAction extends Action{
2
3
4
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
5
6
7
AjaxForm ajaxForm = (AjaxForm)form;
8
System.out.println("Hello " + ajaxForm.getName());
9
jQuery Ajax POST (working):
JavaScript
1
27
27
1
<script src="/BusinessProcess_Project/jquery.js"></script>
2
3
<script type="text/javascript">
4
function doAjaxPost() {
5
// get the form values
6
//var name = $('#name').val();
7
8
var formData ={
9
'name':'jQuery_Oron'
10
};
11
12
$.ajax({
13
type: "POST",
14
url: "/BusinessProcess_Project/AjaxSubmit.do",
15
// data: "name=" + 'jQuery',
16
data:formData,
17
dataType: "text/json",
18
success: function(response){
19
20
},
21
error: function(e){
22
alert('Error: ' + e);
23
}
24
});
25
}
26
</script>
27
AngularJS Ajax POST (not working):
JavaScript
1
32
32
1
<script type="text/javascript">
2
$scope.myShabi = {};
3
4
$scope.myShabi.doClick = function(item, event) {
5
// $scope.init = function(item, event) {
6
$scope.loading = true;
7
8
9
var formData ={
10
'name':'Angular_Oron'
11
};
12
13
var responsePromise = $http({
14
method: 'POST',
15
url: '/BusinessProcess_Project/AjaxSubmit.do',
16
data: formData
17
});
18
19
20
responsePromise.success(function(data, status, headers, config) {
21
$scope.jsonFromServer = data;
22
23
$scope.status ="Finished";
24
$scope.loading = false;
25
});
26
27
responsePromise.error(function(data, status, headers, config) {
28
alert("AJAX failed!");
29
});
30
};
31
</script>
32
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
:
JavaScript
1
9
1
2
var responsePromise = $http({
3
method: 'POST',
4
url: '/BusinessProcess_Project/AjaxSubmit.do',
5
// data: formData
6
params: formData
7
});
8
9