zoukankan      html  css  js  c++  java
  • ajax(xmlHttpRequest对象)的运行原理固定模式

    发出请求

    您已经有了一个崭新的 XMLHttpRequest 对象,现在让它干点活儿吧。首先需要一个 Web 页面能够调用的 JavaScript 方法(比如当用户输入文本或者从菜单中选择一项时)。接下来就是在所有 Ajax 应用程序中基本都雷同的流程:

    1、从 Web 表单中获取需要的数据。 
    2、建立要连接的 URL。 
    3、打开到服务器的连接。 
    4、设置服务器在完成后要运行的函数。 
    5、发送请求。

    清单 5 中的示例 Ajax 方法就是按照这个顺序组织的:

    清单 5. 发出 Ajax 请求

    function callServer() {
      // Get the city and state from the web form
      var city = document.getElementById("city").value;
      var state = document.getElementById("state").value;
      // Only go on if there are values for both fields
      if ((city == null) || (city == "")) return;
      if ((state == null) || (state == "")) return;

      // Build the URL to connect to
      var url = "/scripts/getZipCode.php?city=" + escape(city) + "&state=" + escape(state);

      // Open a connection to the server
      xmlHttp.open("GET", url, true);

      // Setup a function for the server to run when it's done
      xmlHttp.onreadystatechange = updatePage;

      // Send the request
      xmlHttp.send(null);
    }

  • 相关阅读:
    update语句中存在''语法书写方式
    CSS的代码风格
    CSS的语法规范
    CSS层叠样式表导读
    CSS简介
    HTML基本标签(下)
    HTML基本标签(上)
    HTML简介导读
    集合及其运用
    字典的镶嵌
  • 原文地址:https://www.cnblogs.com/zlzlzl1993/p/3316941.html
Copyright © 2011-2022 走看看