ajax异步无刷新

2017-07-15  本文已影响0人  指尖技术

ajax方法实现:可以对ajax进行一下封装,方便各个页面进行调用:

function MyAjax(type, url, callBack, data, dataType, asyncType)
{ 
    if (dataType == null) { dataType = "text"; } 
    if (asyncType == null) {asyncType = true; } 
    $.ajax({ 
        type: type, // post或者get 
        url: url , //url最好加一个url+Math.random(),这样可以保证每次请求的页面被浏览器视为不同
        data: data, //这里是要传递的参数,格式为 data: "{paraName:paraValue}" 
        dataType: dataType, //string,xml,script,json,text
        async:asyncType, //同步异步true /false 
        error: function (XmlHttpRequest, xmlhttp, info) { 
        }, 
        success: function (result) { 
        //回调函数,result,返回值 
        callBack(result); 
        }, 
    }); 
    
}

调用:MyAjax('post',"url?id=" + id, DoOK);
说明:async:true表示异步。此方式是说当ajax发送请求后,在等待server端返回的这个过程中,前台会继续 执行ajax块后面的脚本,直到server端返回正 确的结果才会去执行success,相当于开了两个线程;false是同步,即前台会等待server端返回数据后再执行。

上一篇 下一篇

猜你喜欢

热点阅读