开发者

How to call Struts2 Action method in ajax?

开发者 https://www.devze.com 2023-01-07 04:53 出处:网络
How to call Struts2 Action method in ajax.Still now i worked to call servlet.is there any possibilities?if so plea开发者_运维百科se do share.Update the struts.xml as

How to call Struts2 Action method in ajax.Still now i worked to call servlet.is there any possibilities?if so plea开发者_运维百科se do share.


Update the struts.xml as

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

$.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');

       }
});


Instead of a Servlet you can also call an Struts2 Action.

http://struts.apache.org/2.x/docs/ajax.html

Struts2 supports also different AJAX Frameworks like jQuery/Dojo/YUI with different plugins.

0

精彩评论

暂无评论...
验证码 换一张
取 消