jquery ajax XMLHttpRequest.status=0

在使用jquery ajax进行异步请求的时候,明明数据处理也成功了,返回的json数据也是对的,但是返回状态一直到error里面而不是succes里面。懵逼了一脸。使用google浏览器,查看请求状态,也没看啥异样。这时候,开始仔细分析问题。我的form表单如下:

               <form>
                    <table class="Modal-body u-clearfix u-pb-3" style="height: 600px; width: 100%; overflow: auto;">
                        <tbody>
                            <tr>
                                <td>
                                    <label for="">公司名称:</label>
                                    <input id="companyName" placeholder="公司名称" required="required" spellcheck="false" type="text" style="height: 35px">
                                </td>
                                
                            </tr>
                            <tr>
                                <td>
                                    <label for="">成立日期:</label>
                                    <input id="establishmentDate" required="required" placeholder="格式:2017-xx-xx" οnfοcus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd'})" type="text" style="height: 35px">
                                </td>
                                <td>
                                    <label for="">执照到期:</label>
                                    <input id="licenseExpireDate" required="required" placeholder="格式:2017-xx-xx" οnfοcus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd'})" type="text" style="height: 35px">
                                </td>
                                <td>
                                    <label for="">登记机关:</label>
                                    <input id="registrationAuthority" required="required" placeholder="请输入登记机关" spellcheck="false" type="text" style="height: 35px">
                                </td>
                           </tr>
                        </tbody>
                    </table>
                    <div style="margin-left: 45%; margin-bottom: 20px">
                        <button class="Button Button--blue Button--large Button--fullWidth" style="width: 120px; height: 35px; line-height: 0px" type="submit" οnclick="submitMember()" >
                            提交
                        </input>
                    </div>
                </form>

为了使用使用input里面的required=“required”,所以按钮必须使用button/input type="submit"而不是input type="button",但是走到这,问题已经凸显出来了,既然提交按钮是button,那点击按钮的时候,直接就走from表单提交了,这样就会把按钮里面的cliick事件给冲突调,这样点击button的时候,直接走form表单提交。ajax自然状态为0了。至于button和input的区别自己百度吧。所以问题出来了就把问题解决了就行了。把button这个按钮让他不走form表单,走click事件。解决办法如下:

//阻止form的submit事件
$(document).ready(function () {
    $("form").submit(function (e) {
        e.preventDefault();
    });
});
这样,from表单就不会提交,走button中的click事件了。这样ajax就可以调用成功了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值