您的位置:首页 > 理论基础 > 计算机网络

jquery ajax XMLHttpRequest.status=0

2017-12-19 11:09 441 查看
在使用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" onfocus="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" onfocus="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" onclick="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就可以调用成功了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息