Chathurika Madhubhashini Chathurika Madhubhashini -4 years ago 157
Ajax Question

jQuery Ajax request to Struts2 action class always returns 200 ok, but error event is fired

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download