原生js实现Ajax 发表于 2018-05-16 | 更新于: 2022-03-04 | 分类于 javaScript相关 | 阅读次数: 字数统计: 248 原生js实现Ajax1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465function ajax(){var ajaxData = { type:arguments[0].type || 'GET', url:arguments[0].url || '', aysnc:arguments[0].async || 'true', data:arguments[0].data || null, dataType:arguments[0].dataType || 'text', contentType:arguments[0].contentType || 'application/x-www-form-urlencoded', beforeSend:arguments[0].beforeSend || function(){}, success:arguments[0].success || function(){}, error:arguments[0].error || function(){},}ajaxData.beforeSend(){ var xhr = createxmlHttpRequest(); xhr.responseType = ajaxData.dataType; xhr.open(ajaxData.type,ajaxData.url,ajaxData.async); xhr.setRequestHeader("Content-Type",ajaxData.contentType); xhr.send(convertData(ajaxData.data)); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if(xhr.status == 200){ ajaxData.success(xhr.response) }else{ ajaxData.error() } } }}function createxmlHttpRequest() { if (window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { return new XMLHttpRequest(); } } function convertData(data){ if( typeof data === 'object' ){ var convertResult = "" ; for(var c in data){ convertResult+= c + "=" + data[c] + "&"; } convertResult=convertResult.substring(0,convertResult.length-1) return convertResult; }else{ return data; } }}// 调用ajax({ type:"POST", url:"ajax.php", dataType:"json", data:{"val1":"abc","val2":123,"val3":"456"}, beforeSend:function(){ //some js code }, success:function(msg){ console.log(msg) }, error:function(){ console.log("error") } }) 本文作者: 赖彬鸿 本文链接: http://laibh.top/2018-05-16-originjsAjax.html 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!