ajax 如何在ajax中调用Struts2 Action方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3239960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call Struts2 Action method in ajax?
提问by Jothi
How to call Struts2 Action method in ajax.Still now i worked to call servlet.is there any possibilities?if so please do share.
如何在 ajax 中调用 Struts2 Action 方法。现在我还在努力调用 servlet。是否有任何可能性?如果有,请分享。
采纳答案by Johannes
Instead of a Servlet you can also call an Struts2 Action.
除了 Servlet,您还可以调用 Struts2 操作。
http://struts.apache.org/2.x/docs/ajax.html
http://struts.apache.org/2.x/docs/ajax.html
Struts2 supports also different AJAX Frameworks like jQuery/Dojo/YUI with different plugins.
Struts2 还支持不同的 AJAX 框架,如带有不同插件的 jQuery/Dojo/YUI。
回答by bad potato
Update the struts.xmlas
更新struts.xml为
<package name="prjajax" namespace="/" extends="json-default">
<result-types>
<result-type name="json" class="org.apache.struts2.json.JSONResult" />
</result-types>
<action name="AJAXAction" class="com.demo.sd.prj.ui.actions.AJAXAction" method="myMethod">
<result name="success" type="json" />
</action>
jQuery code:
jQuery code:
$.ajax({
url: "AJAXAction",
type: "POST",
data: {data: $('#txtbox').val()},
dataType: "json",
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('Error ' + textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
},
success: function(data){
alert('SUCCESS');
}
});

