function zhankai(id){/*函数:展开某个分类*/ var httpRequest = new XMLHttpRequest () ; httpRequest.open('GET',"/d/js/class/class"+id+"_newnews.js", true); httpRequest.send(); httpRequest.onreadystatechange = function () { if (httpRequest.readyState == 4 && httpRequest.status == 200) { var json = httpRequest.responseText;//获取到json字符串,还需解析 json=json.replace('document.write\("<ul>',""); json=json.replace('</ul>"\);',""); json=json.replace(/\\/g,"");/*替换反斜杠*/ document.getElementById("zhanwei_"+id).innerHTML=json; document.getElementById("zhanwei_"+id).previousElementSibling.previousElementSibling.className="jianhao"; } } }
function shengcheng(){ var httpRequest = new XMLHttpRequest() ; httpRequest.open('POST',"admin/function.pinyin.php", true); data = new FormData(); data.append("user", "张三"); httpRequest.send(data); httpRequest.onreadystatechange = function () { if (httpRequest.readyState == 4 && httpRequest.status == 200) { var str = httpRequest.responseText; document.getElementById("keywordspinyin").value=str; } } }
注意事项:
默认只能请求同一个域名,如果跨域就会出错,所以跨域的话可以增加协议头
或者在api接口的php文件里设置协议头.代码如下:
header("Access-Control-Allow-Origin:*");//解决跨域问题,允许任意域名请求此接口