JQuery Ajax request to Struts2 action class always returns 200 ok but error event fired

I'm new to Struts2 and I'm about to submit a form dataset to the 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");
        }
});
      

Run codeHide result


This is mine 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>

      

The Ajax request returns 200 OK but always warns "Error". I have referenced many articles but still haven't got the right solution.

+3


source to share


2 answers


You are posting the result redirect

when you make an ajax request. The Ajax request does not redirect you to another location, it will stay on the same page when the request is made.

Change

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

      



to

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

      

dispatcher

is the default result type that will render the JSP at the specified location and write the HTML response to the output. This answer can be easily downloaded using jQuery to anyone div

available on the same page.

+1


source


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

So you need to check the server side code why the answer is empty or contact the developer on the same server.



For more calcification, you can use the code below.

$.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);
    }
});

      

+1


source







All Articles