Search code examples
javascriptjspstruts-1

How to call struts action from plain javascript file


This query is related to struts 1.3.

Let's say I have one action called 'getName.do' which is mapped to 'GetName.java' action class. In the dmexecute method of this action class, I am setting one result say String result = Hello;.

My query is how I can call this struts action (getName.do?parameter=value) from one of my javascript files.

Essentially, I want the value of result, which is Hello, to be present in my javascript file through this getName.do?parameter=value call. How I can make this call in my javascript file.

Any help will be highly appreciated.


Solution

  • You can use jQuery ajax for this purpose.

    Importing jquery file:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    

    Calling action through jquery ajax:

    <script type="text/javascript">
    $.ajax({
        url : "getName.do?parameter=value",
        type : "POST",
        success : function(data) {
            // You'll get your response here
            alert(data);
        }
    });
    </script>
    

    Hope this will help.