0

Hi I want call struts2 action using jquery ajax,Am able to call only Servlet using ajax. Please any one help on this. Here is my code:

My struts.xml

  <?xml version="1.0" encoding="UTF-8" ?>
   <!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      "http://struts.apache.org/dtds/struts-2.0.dtd">

   <struts>
      <package name="view" extends="struts-portlet-default"
    namespace="/view">

   <action name="bcmPMPF"
     class="com.ibm.pm.action.PMPeriodicAction" method="prepare">
     <result name="success">/_JQGridPorletCrudColumns/jsp/html   
          /JQGridPorletCrudColumnsPortletView.jsp</result>
       </action>
         <action name="bcmPMPFC.action"
          class="com.ibm.pm.action.PMPeriodicColumnAction" method="prepare">
          <result name="success">/_JQGridPorletCrudColumns/jsp/html
          /JQGridPorletCrudColumnsPortletView.jsp</result>
    </action>

    </package>
    </struts> 

      and my jsp page is:
          <SCRIPT LANGUAGE="JavaScript">
     var colModel;
   var colNames;
    var
 urlPath='<%=renderResponse.encodeURL(renderRequest.getContextPath())%>'+"/BCMRoleInstanceServlet";
      var urlColPath='<%=renderResponse.encodeURL(renderRequest.getContextPath())%>'+'/view/bcmPMPFC';
        $(document).ready(function(){

         $("#srid").live('change',(function(event){

        $.ajax(
          {
        type: "POST",
       url: urlColPath,
         data: "",
        dataType: 'json',
         success: function(result)
                {

            alert("result:"+result);


             }
           });

         }));
Rakesh Sabbani
  • 1,655
  • 5
  • 17
  • 31

1 Answers1

1

Make sure you have "traditional: true" set in the ajax call otherwise struts2 by default will not parse arguments passed to it... something like:

$.ajax({
    tradional: true,
    type: "POST",
    url: urlColPath,
    data: "",
    dataType: 'json',
    success: function(result)
    {
        alert("result:"+result);
    }
});

Also to make sure the url is correct consider using the action tag:

url: '<s:url namespace="/somePlace" name="someAction"/>',

The above creates something like:

http://whatEverYourContextRootIs:8080/somePlace/someAction.action

Quaternion
  • 10,380
  • 6
  • 51
  • 102
  • Sorry Quaternion,I want to call the struts2 action in WebSphere Portal,and the url should not be as you mentioned,my basic url looks like http://localhost:10040/wps/myportal and in this the url is encrypted by the portal server itself that's why am unable to call the struts action,can you trace if possible – Rakesh Sabbani Jul 17 '11 at 09:32
  • No, I had written s:action when I meant to say s:url... I edited the answer to correct that. Sorry for the confusion. Anyways try it out to see what kind of string it constructs, it is better than hard coding the value because that way it will change if you move it to a different server. I don't really understand what you mean by an encrypted url. – Quaternion Jul 17 '11 at 17:52