jquery中$.post()和$.ajax()总结

$.post()方法中的function(){}只有当请求有响应回来才能执行该方法,如果需要在请求出错时执行函数,则只能使用 $.ajax();

举个例子:我请求的路径出错的时候

 $.post("${pageContext.request.contextPath}/blogger/loginOu.html",{},
function(data){
if(data.msg="success"){alert("成功")}
 else{alert("提交失败");}
});

上面的路径是404的

控制台那边提示404,但是页面没有任何提示,要想请求出错就提示的话就要改成用$.ajax()

$.ajax(
    {
        url:"${pageContext.request.contextPath}/blogger/loginOu.html",//发送的路径
        data:{},//发送的数据
        type:"post",//发送的方式
        dataType:"json",//服务器返回的数据类型
        success: function(data) {
            if(data.msg="success"){
                alert("已提交成功");
               
            }else{
                alert("提交失败");
            }
        },
        error: function (data){
            alert("提交失败");
        }

    });

知道区别的话使用时候可以看情况用

猜你喜欢

转载自blog.csdn.net/linlinlinfeng/article/details/85052289