楼主我觉得你可能理解错了ajax的基本作用了。
你看这个,用Get方式传了一个或者一群值,存在了/ajax_info.txt 里这个文件我本来觉得应该被下载到了我的浏览器缓存,我在我的系统里,搜这个文件,我也找不到.请问我怎么看这个文件里面写的什么?
其实ajax是用来请求数据的,而不是用来写数据的.
比如下面这个封装好的ajax方法
function ajax(method, url, data, success) {
var xhr = null;
try {
xhr = new XMLHttpRequest();
} catch (e) {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
if (method == 'get' && data) {
url += '?' + data;
}
xhr.open(method,url,true);
if (method == 'get') {
xhr.send();
} else {
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
xhr.send(data);
}
xhr.onreadystatechange = function() {
if ( xhr.readyState == 4 ) {
if ( xhr.status == 200 ) {
success && success(xhr.responseText);
} else {
alert('出错了,Err:' + xhr.status);
}
}
}
}
假设ajax_info.txt文件里面存的内容是hello world
有下面的调用方式
ajax('get', '/try/ajax/ajax_info.txt', function(data){
console.log(data); // 'hello world'
})