ajax中get和post的提交、却别、错误处理以及注意事项

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

$.get和$.post的不一样
    一、get经过url提交的,post是经过http消息实体提交的
    二、get提交大小限制为2kb,post不限制
    三、get提交会被缓存下来,有安全隐患,post没有

    $.get 和 $.post的几种传参方式
    一、在url后面直接问号传参: test.php?age=20
    二、以字符串键值对的方式传参:  'age=20'
    三、以对象键值对的方式传参:  {age:20}

    $.get 以上三种方式都支持,可是$.post和$.ajax只支持后2种写法

    具体demo:
    一、$.get('test.php?age=20',function(result){
        alert(result)
    })
    二、$.get('test.php','age=20',function(result){
        alert(result)
    })
    三、$.get('test.php',{age:20},function(result){
        alert(result)
    })

   3、getScript 通常就是引入一个js文件
   $.getScript('demo.js') 便可 
   四:getScript 请求一个json文件,没必要要指定返回的数据类型,并且若是制定了非json的type类型如type:html ,因为安全性高一点也不会返回html格式的数据

   5、在用ajax 提交表单的时候能够用表单序列化获取表单的传参内容,并且传参的形式是字符串键值对,而且还会对url进行编码
   $('form').serialize();
   如:$.ajax({
        type:'POST',
        url:'text.php',
        data: $('form').serialize(),
        success:function(response,status,xhr){
            dosomething...
        }  
   })

   五-1;在一下html中能够用decodeURLComponent对序列化的数据进行解码
   <form>
       <input type="checkbox" name="sex" value="男" id="">
       <input type="checkbox" name="sex" value="女" id="">
   </form>
   <div id="box"></div>
   <script>
        $('form input[name=sex]').click(function(){
            $('#box').html(decodeURLComponent($(this).serialize()))
        })
   </script>


   5-2,以上的例子能够用serializeArray()可将数据格式化为json格式


   六;ajaxSetup 是对ajax进行初始化,应用场景当多个ajax重复用到某些数据的时候能够分装起来如:
   $.ajaxSetup({
    type:'POST',
    url:'text.php',
    data:'{}'
   });
   $('form :submit').click(function(){
    $.ajax({
     success:function(response,status,xhr){


     }
    })
   });

   7.$.param()方法可对复杂的json进行字符串键值对解析r

   进阶:
   八、$.ajaxstart()和$.ajaxStop()放置加载时间过长处理
   在jq1.8版本后必须绑定在document上如:
   $(document).ajaxStart(function(){
        $('.loading').show()
   }).ajaxStop(function(){
         $('.loading').hide()
   });

php

   8-1 若是加载超时,能够用timeout设置超时限制html


 ===============
    $.ajax进阶 
    一、加载请求
    $.ajaxStart() 、$.ajaxStop 能够在对用户等待时间加载loading图片


    二、错误处理
    $.ajax的错误处理  能够在本身当前添加error方法,参数是(xhr,status,statusText) 如:
    2-1: $.ajax的error处理
    $.ajax({
        url:'www.seogjgsdggd.com/test.php',
        type:'POST',
        data:'age=20',
        error:function(xhr,status,statusText){
            alert(xhr.status)
        }
    });
    2-1:$.post的error错误处理,添加连缀的局部方法error,参数是(xhr,errorText,errorType)如:
    $.post('test.php','age=20',function(response,status,xhr){
         alert(response+"//"+xhr.status)
    }).error(function(xhr,errorText,errorType){
        alert('错误')
    });
    也能够用全局的ajaxError方法如(1.8版本建议吧事件绑定在document上),可是参数有所不一样(event,xhr,settings,errorType) 如:
    $(document).ajaxError(function(event,xhr,settings,errorType){
        event,xhr,settings都是对象,event通常就用(type,target)属性,settings通常就用(url,type);
    });

    3/ 请求全局事件有 $.ajaxstart(),$.ajaxstop()、$.ajaxError(),
                        $.ajaxSuccess(),$.ajaxComplete,$.ajaxSend()
        前三个是请求时出发的全局事件,
        $.ajaxSuccess() 对应一个局部方法 .success();
        $.ajaxComplete()对应一个局部方法 .complete();
        $.ajaxSend()没有对应的局部方法,只有属性beforeSend
        例子:
        $(document).ajaxSend(function(){
            alert(发送请求以前);
        }).ajaxComplete(function(){
             alert(请求成功和失败以后都会出现,是出如今成功或者失败的后面);
        }).ajaxSuccess(function(){
             alert(请求成功后);
        }).ajaxError(function(){
            alert(请求失败后);
        })

        $.post('test.php',$('form').serialize()).success(function(){
            alert(请求成功后);
        }).complete(function(){
            alert(请求完成后);
        }).error(function(){
            alert(请求失败后);
        })java


        $.ajax({
            url:'test.php',
            type:'POST',
            data:$('form').serialize(),
            success:function(response,status,xhr){
                alert(请求成功后);
            },
            complete:function(){
                alert(请求完成后);
            },
            error:function(xhr,errorText,errorType){
                alert(请求错误后);
            },
            beforSend:function(){
                alert(请求以前);
            }
        })
ajax


</body>
</html>json

相关文章
相关标签/搜索