简体   繁体   English

对Struts2动作类的jQuery Ajax请求总是返回200 ok,但是会触发error事件

[英]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. 我是Struts2的新手,我将把一组表单数据发送到struts动作类。

This is my Ajax code in jsp 这是我在jsp中的Ajax代码

 $.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 : 这是我的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". Ajax请求返回200 OK,但始终警告“错误”。 I referred many articles but still not get proper solution 我提到了许多文章,但仍未得到适当的解决方案

You are sending result redirect while doing ajax request. 您在执行ajax请求时发送结果redirect Ajax request won't redirect you to another location, it will stay on the same page when request was made. Ajax请求不会将您重定向到另一个位置,它会在请求时保留在同一页面上。

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. dispatcher是默认结果类型,它将在指定位置呈现JSP并将响应HTML写入out。 This response could be easily loaded with jQuery to any div available on the same page. 可以使用jQuery轻松地将此响应加载到同一页面上的任何div

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. 是的,您可能会得到响应200 OK仍然在ajax请求中执行错误回调,因为来自请求的URL的响应为空。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM