Akhilesh N
Akhilesh N

Reputation: 69

Unable to call action using Ajax + JSON

In my Ajax request I am calling action class like this:

var ajaxRequest;
ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("GET", "../displayProjects.action?userMasterID=" + userMasterDTO.id );
ajaxRequest.send(null);

And my struts.xml is as below:

<package name="default" extends="json-default">
  <action name="displayProjects"
      class="com.activitymanager.action.ExpenseTrackerAction"
      method="doGetProjectDetails">
    <result type="json"/>
  </action>
</package>

I'm unable to call the action class using above JavaScript. How do I get this working?

Upvotes: -1

Views: 574

Answers (4)

DarkHorse
DarkHorse

Reputation: 2770

Why arent you using Jquery Ajax apis for your ajax call's.. Its makes things so easy :) Example:

function Ajaxcall() {

        var xhttp = $.ajax({
            success : function(response) {
                alert("Ajax call was Successful");

            },
            url : "someAction.action",
            type : "POST",
            async : true,
            data : "name=" + str

        });

    }

and your struts.xml file need to be like this:

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="default" extends="struts-default,json-default" namespace="/">
<action name="displayProjects"
      class="com.activitymanager.action.ExpenseTrackerAction"
      method="doGetProjectDetails">
    <result name="success" type="json">/path/to/JSP-file/making/ajax/call</result>
  </action>
</package>
</struts>

Upvotes: 0

Roman C
Roman C

Reputation: 1

Just type a correct url to your action

ajaxRequest.open("GET", "/displayProjects.action!doGetProjectDetails?userMasterID=" + userMasterDTO.id );

Upvotes: 0

jfrank
jfrank

Reputation: 733

Use Firebug's Network tab or some such tool to see the ajax request that you are sending. Then try putting that same request URL into the browser's address bar and see if it works. My guess is that it has to do with the "../" that you put in the URL.

Upvotes: 1

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

You may want to use Jquery to handle your AJAX calls. Please refer to this answer: Stackoverflow #12137803.

Upvotes: 1

Related Questions