3

I am new to Struts2 and I'm going to send set of form data to struts action class.

This is my Ajax code in jsp

$.ajax({
    method: "POST",
    url: "getProjectPost",
    data: { "language" : langArr , "clientId": clientId, "projectName": projectName, "projectType": projectType},
    traditional: true,
    success:
        function()
        {
     alert("Success");
        },
    error: 
    function()
        {
      alert("Error");
        }
});

This is my struts.xml:

<package name="projectPost" namespace="/" extends="struts-default">
    <action name="getProjectPost" class="com.test.ProjectPostAction" method="execute">
        <result name="success" type="redirect">
        <param name="location">/webpages/Client/Success.jsp</param >
        </result>
        <result name="failure">./LandingPage.jsp</result>
        <result name="error">./error.jsp</result>
    </action>
</package>

Ajax request returns 200 OK, but always alert "Error". I referred many articles but still not get proper solution

Roman C
  • 49,761
  • 33
  • 66
  • 176
WCM
  • 595
  • 3
  • 11
  • 27

2 Answers2

2

You are sending result redirect while doing ajax request. Ajax request won't redirect you to another location, it will stay on the same page when request was made.

Change

<result name="success" type="redirect">

to

<result name="success" type="dispatcher">

The dispatcher is default result type that will render JSP at the specified location and write response HTML to the out. This response could be easily loaded with jQuery to any div available on the same page.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • tried and alerted the response in error event of ajax. it give {"readyState":0, "response Text":"", "status":0, "statusText":"error"} – WCM Mar 22 '17 at 11:18
  • 1
    It is working.The problem was I didn't call e.preventDefault() inside of my event handler. Thank you – WCM Mar 23 '17 at 03:38
1

Yes, it is possible that you get response 200 OK still error callback is executed in ajax request because response from requested url is empty.

So you have to check server side code why response is empty or contact to back-end developer for same.

For more calcification you can use below code.

$.ajax({
    method: "POST",
    url: "getProjectPost",
    data: { "language" : langArr , "clientId": clientId, "projectName": projectName, "projectType": projectType},
    success: function(data){
        console.log("in success function");
        alert("Error:: " + data);
    },
    error: function(data){
         console.log("in error function");
         alert("Error :: "+ data);
    }
});
Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42
  • alerted the response in error event of ajax. it give {"readyState":0, "response Text":"", "status":0, "statusText":"error"}. So what can I do now – WCM Mar 22 '17 at 11:47