gpt4 book ai didi

javascript - 在 Struts 1 中使用 AngularJS 进行 Ajax POST

转载 作者:行者123 更新时间:2023-11-30 17:16:23 25 4
gpt4 key购买 nike

我正在尝试使用 Struts 操作表单发送 Ajax POST。

我已经使用 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(有效):

    <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(工作):

    <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>

出于某种原因,AngularJS 不喜欢我发送 ActionForm 数据的方式。

最佳答案

您应该使用的不是data,而是params:

...
var responsePromise = $http({
method: 'POST',
url: '/BusinessProcess_Project/AjaxSubmit.do',
// data: formData
params: formData
});
...

关于javascript - 在 Struts 1 中使用 AngularJS 进行 Ajax POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26056382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com