前端发送ajax请求,并采用restful风格。
var xhr = createXHR(); xhr.onreadystatechange = function(){ if(xhr.readyState === 4){ if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){ //success //console.log(xhr.responseText);
//成功之后的操作
gridStore.setData( JSON.parse(xhr.responseText) ); } else { console.log("Request was unsuccessful: " + xhr.status); } } }; xhr.open('get','diana',true); //第三个参数 ,是否异步 xhr.send(null); function createXHR(){ if (typeof XMLHttpRequest != "undefined"){ return new XMLHttpRequest(); } else if (typeof ActiveXObject != "undefined"){ if (typeof arguments.callee.activeXString != "string"){ var versions = [ "MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"], i, len; for (i=0,len=versions.length; i < len; i++){ try { new ActiveXObject(versions[i]); arguments.callee.activeXString = versions[i]; break; } catch (ex){ //跳过 } } } return new ActiveXObject(arguments.callee.activeXString); } else { throw new Error("No XHR object available."); } }
以上便是原生ajax请求的简单使用。