js纯ajax

时间:2023-03-09 07:02:37
js纯ajax
  1. var XMLHttpReq;
  2. function createXMLHttpRequest() {
  3. try {
  4. XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");//IE高版本创建XMLHTTP
  5. }
  6. catch(E) {
  7. try {
  8. XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");//IE低版本创建XMLHTTP
  9. }
  10. catch(E) {
  11. XMLHttpReq = new XMLHttpRequest();//兼容非IE浏览器,直接创建XMLHTTP对象
  12. }
  13. }
  14. }
  15. function sendAjaxRequest(url) {
  16. createXMLHttpRequest();                                //创建XMLHttpRequest对象
  17. XMLHttpReq.open("post", url, true);
  18. XMLHttpReq.onreadystatechange = processResponse; //指定响应函数
  19. XMLHttpReq.send(null);
  20. }
  21. //回调函数
  22. function processResponse() {
  23. if (XMLHttpReq.readyState == 4) {
  24. if (XMLHttpReq.status == 200) {
  25. var text = XMLHttpReq.responseText;
  26. /**
  27. *实现回调
  28. */
  29. text = window.decodeURI(text);
  30. var cp = document.getElementById("cp");
  31. cp.innerHTML = "";
  32. var values = text.split("|");
  33. for (var i = 0; i < values.length; i++) {
  34. var temp = document.createElement("option");
  35. temp.text = values[i];
  36. temp.value = values[i];
  37. cp.options.add(temp);
  38. }
  39. }
  40. }
  41. }