weixin_33691598 2015-03-28 15:12 采纳率: 0%
浏览 175

AJAX Raw Javascript Basic

i wonder why the open and send method when we using ajax comes at the end. Not before the responseText method

function loadXMLDoc()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","xmlhttp_info.txt",true);
xmlhttp.send();
}

i'm a bit confused with the open method which take the url of our data/file. And how could the xmlhttp.responsetext method now which file we're working with, since the code is at the bottom –

  • 写回答

2条回答 默认 最新

  • weixin_33712987 2015-03-28 15:18
    关注

    Basically we need to construct the request at first and bind necessary event handlers to process the response. Only then we need to fire the request. That's the reason why it's at the end.

    if we fire the request at first, then we might not have any event handlers registered to handle the response.

    So that's the reason why at first we are constructing the XHR object and binding the event hanlders using xmlhttp.onreadystatechange.

    评论

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败