【发布时间】:2015-12-21 14:29:05
【问题描述】:
我正在尝试使用 ajax 从我的 jsp 中调用 struts2 动作类。我可以点击动作类并能够将参数传递给动作类。但是动作类对我的 ajax 请求的响应始终为空
所有 setter 和 getter 都设置正确,当我在 debug sysouts 中看到它们工作正常
动作类
@Override
@Action(value = "/search",
results = { @Result(name = "success", type="json")})
public String execute() throws ParseException
{
this.setName(this.term+": "+this.pos);
System.out.println("Name: "+Name);
JSONObject json = (JSONObject)new JSONParser().parse(Name);
return ActionSupport.SUCCESS;
}
jsp
function Search()
{
var term = document.getElementById("term").value;
var pos = document.getElementById("pos").value;
var itr = document.getElementById("itr").value;
var pri = document.getElementById("pri").value;
var atb = document.getElementById("atb").value;
var jsonData = {};
jsonData.term = term;
jsonData.pos = pos;
jsonData.itr = itr;
jsonData.pri = pri;
jsonData.atb = atb;
$.ajax({
type: "POST",
url: "search",
dataType: "json",
data: jsonData,
success: function(response){
console.log(""+response);
alert('Name: '+response.Name);
//alert('Length: '+data[0].length);
/* $.each(data[0],function(index, value){
alert("value " +value);
});*/
},
error: function(response){
alert(response);
alert(response.length);
$.each(response,function(index, value){
alert("value " + value);
});
}
});
}
我可以看到响应始终为空。我不确定出了什么问题,但似乎编码部分是正确的。我在ajax调用中做错了吗?
【问题讨论】:
-
控件是进入错误函数还是成功函数?
-
你试过从
execute()返回null吗? -
@Mihir: 控制只进入成功功能
-
@Rahul:是的,我尝试从 action 返回 null ,但我只能看到响应为 null
-
@SrinivasRaju 成功不是一个函数,而是一个结果代码,用于执行动作配置中的某些结果意味着成功。也就是这个结果负责初始化响应并通过http连接发送。