httpError: The Http Transport returned a 0 status code

httpError: The Http Transport returned a 0 status code.  This is usually the result of mixing ajax and full requests.  This is usually undesired, for both performance and data integrity reasons.

I have a login page:

<h:form id="f_login" >
    <f:passThroughAttribute name="data-ajax" value="false" />

    <h:messages id="message" showDetail="false" errorClass="error_msg" fatalClass="success_msg" infoClass="info_msg" warnClass="warning_msg" />

    <h:inputText value="#{loginWEB.user.email}" >
        <f:passThroughAttribute name="placeholder" value="Email" />
        <f:ajax event="change" execute="@this" />
    </h:inputText>

    <h:inputSecret value="#{loginWEB.user.senha}" autocomplete="off" >
        <f:passThroughAttribute name="placeholder" value="Senha" />
        <f:ajax event="change" execute="@this" />
    </h:inputSecret>

    <h:commandButton id="btn_login" value="Login" action="#{loginWEB.login()}" >
        <f:passThroughAttribute name="data-theme" value="g" />
        <f:ajax render="message" />
    </h:commandButton>
</h:form>
With this action method:

public void login() {
    try {

        FacesContext context = FacesContext.getCurrentInstance();

        if (...) { //Verify email and password
            context.getExternalContext().redirect("/templates/main.xhtml"); 
        }
        else {
            FacesMessage message = new FacesMessage("Invalid email or password");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(null, message);
        }

    } catch (Exception e) {
        Logger.getLogger(LoginWEB.class.getName()).log(Level.SEVERE, null, e);
    }
}

Submitting this form sometimes causes a JS error:

httpError: The Http Transport returned a 0 status code. This is usually the result of mixing ajax and full requests. This is usually undesired, for both performance and data integrity reasons.


 Answers

There's indeed chance on that specific error if one of the input fields has still focus while it's changed and the submit button is pressed by mouse or [enter] key instead of being tabbed to (so that the input field's change event triggers first). A race condition will occur between 2 ajax requests: the one triggered by input field's change event and the other triggered by submit button's action event. It's in this kind of situation undetermined which one would hit the server first.

In JSF, ajax requests are queued. So, if one hits the server, the other will wait until it returns and completes. Your problem will occur when the ajax request of the submit button is the first one which hits the server. In case of a successful login, the server will return an instruction to perform a redirect. This redirect is handled by JavaScript during the complete of the ajax request. After this, the next ajax request in the queue, if any, will be fired (in spite of the redirect! which is in turn IMO a design error in JSF's ajax engine, but that aside). However, since the document has changed by a redirect, the second ajax request misses hit, causing this error.

All in all, this ajax form approach is somewhat strange. The input fields and submit button are each individually sending their value by ajax instead of all at once via the submit button. It looks very much like that you initially had that <f:ajax> in the submit button, but you figured that the input values weren't processed, so you decided to add another <f:ajax> to those input fields.

This is not entirely the right approach. You should be using execute="@form" in the submit button in order to instruct it to execute (process) the entire form.

Thus, so:

<h:form id="f_login">
    <f:passThroughAttribute name="data-ajax" value="false" />

    <h:messages id="message" showDetail="false" errorClass="error_msg" fatalClass="success_msg" infoClass="info_msg" warnClass="warning_msg" />

    <h:inputText value="#{loginWEB.user.email}">
        <f:passThroughAttribute name="placeholder" value="Email" />
    </h:inputText>

    <h:inputSecret value="#{loginWEB.user.senha}" autocomplete="off">
        <f:passThroughAttribute name="placeholder" value="Senha" />
    </h:inputSecret>

    <h:commandButton id="btn_login" value="Login" action="#{loginWEB.login}">
        <f:passThroughAttribute name="data-theme" value="g" />
        <f:ajax execute="@form" render="message" />
    </h:commandButton>
</h:form>
This way only one ajax request will be fired at once, eliminating the risk for race conditions.











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值