javascript - AJAX onreadystatechange 的小问题

标签 javascript ajax

xmlhttp.onreadystatechange=function() {

因此这表示 onreadystatechange,调用 function()。我可以将参数放在 function() 中吗?第二个问题,有人写 xmlhttp.onreadystatechange=statechanged 是什么意思?这是否意味着它永远是真的或什么?

最佳答案

  1. 您不能使用参数,因为 onreadystatechange 没有可提供的参数。你期望什么参数?它只是一个处理响应的钩子(Hook)。您可用的是xmlhttp.readyState,它告诉您响应是否准备就绪,xmlhttp.status - http 状态代码(即 200)和 xmlhttp.responseText - 响应本身。

  2. 否 - 这意味着您正在将变量引用(函数)分配给 onreadystatechange

我强烈建议使用 JS 框架(例如 jQuery )进行 AJAX 调用 - 它会抽象出您所询问的低级细节。如果您必须使用 native JS AJAX 调用 - read this tutorial .

关于javascript - AJAX onreadystatechange 的小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2847240/

相关文章:

javascript - 有没有办法在没有 Google Cloud Console 的情况下通过 JavaScript 使用 Google Drive?

javascript - 转换 CSS :active to Javascript . onclick

php - 如何在 woocommerce ajax add_to_cart 之后运行我的自定义 ajax?

jquery - 使用 ASP.NET MVC 和 AJAX 的分页表

javascript - Heroku 应用程序缺少 HTML 模块?

javascript - 降低鼠标速度

javascript - Visual Studio cordova,在 Android 设备上到 "live reload"的简单方法?

php - Ajax 表单只能工作一次

jquery - nodejs express, ajax 发布 w/jquery 并接收响应

javascript - 谷歌地图、SQL、XML、Ajax 天哪!