啊啵哩啵Bou 2021-04-10 13:38 采纳率: 100%
浏览 78
已采纳

Ajax 调试就执行success,不调试就不执行success

Ajax代码
//发送数据请求
    ajax({
        async:false,
        url:'http://localhost:9000/register',
        type:'POST',
        dataType:'json',
        data: JSON.stringify(data),
        success:function(msg){
            var json = JSON.parse(msg);
            alert("注册成功!");
            if (json.status == "true") {
                    window.location.href = "http://localhost:9000/login";
                }
        },
        error:function (error) {
            alert("error"+error.message);
        }
    });

返回数据

msg="{status:"true"}"

而且也不执行ERROR,就直接把success和error跳过了

 

创建Ajax函数,各位前端大佬,帮忙看一下!前端小白甚是感激!

//创建ajax函数
function ajax(options){
    options=options||{};
    options.type=(options.type||'GET').toUpperCase();
    options.dataType=options.dataType||'json';
    params = options.data;
    //创建-第一步
    var xhr;
    //非IE6
    if(window.XMLHttpRequest){
        xhr=new XMLHttpRequest();
    }else{
        //ie6及其以下版本浏览器
        xhr=ActiveXObject('Microsoft.XMLHTTP');
    }

    //接收-第三步
    xhr.onreadystatechange=function(){
        if(xhr.readyState==4){
            if(xhr.status>=200 && xhr.status<300){
                options.success&&options.success(xhr.responseText,xhr.responseXML);
            }else{
                options.error&&options.error(status);
            }
        }else {
            // alert("系统繁忙,请稍后再试!")
        }
    }

    //连接和发送-第二步
    if(options.type==='GET'){
        xhr.open('GET',options.url+'?'+params,true);
        xhr.send(null);
    }else if(options.type=='POST'){
        xhr.open('POST',options.url,true);
        //设置表单提交时的内容类型
        xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        xhr.send(params);
    }
}
  • 写回答

8条回答 默认 最新

  • 啊啵哩啵Bou 2021-04-10 15:59
    关注

    各位老师大牛,我找到问题所在了。因为按钮是bootstrap的button,点击会触发bootstrap的强制刷新,倒是页面被刷新。将<button></button>修改为<button type="button"></button>就可以了。非常感谢各位老师大牛!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊