zoukankan      html  css  js  c++  java
  • Ajax

    1. XMLHttpRequest is the object that enables the JavaScript code to make asynchronous HTTP server requests. This allows you to initiate HTTP requests and receive responses from the server in the background, without requiring the user to submit the page to the server. This feature, combined with the possibility to manipulate the web page using DOM and CSS, allows you to implement responsive functionality and visual effects backed with live data from the server, without the user experiencing any visual interruptions. This is AJAX.
    2. From: http://www.cristiandarie.ro/asp-ajax/Async.html
    3. The typical sequence of operations when working with XMLHttpRequest is as follows:
      1. Create an instance of the XMLHttpRequest object.
      2. Use the XMLHttpRequest object to make an asynchronous call to a server page, defining a callback function that will be executed automatically when the server response is received.
      3. Read the server's response in the callback function.
      4. Update the web page using the data received from the server.
      5. Go to step 2
    4. The following JavaScript function creates an XMLHttpRequest instance by using the native object if available, or the Microsoft.XMLHttp ActiveX control for visitors that use Internet Explorer 6 or older:
      // creates an XMLHttpRequest instance
      function createXMLHttpRequestObject()
      {
        // xmlHttp will store the reference to the XMLHttpRequest object
        var xmlHttp;
        // try to instantiate the native XMLHttpRequest object
        try
        {
          // create an XMLHttpRequest object
          xmlHttp = new XMLHttpRequest();
        }
        catch(e)
        {
          // assume IE6 or older
          try
          {
            xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
          }
          catch(e) { }
        }
        // return the created object or display an error message
        if (!xmlHttp)
          alert("Error creating the XMLHttpRequest object.");
        else 
          return xmlHttp;
      }
    5. This function uses the JavaScript try/catch construct, which is a powerful exception-handling technique that was initially implemented in OOP (Object Oriented Programming) languages. Basically, when an error happens at run time in the JavaScript code, an exception is thrown. The exception is an object that contains the details of the error. Using the try/catch syntax, you can catch the exception and handle it locally, so that the error won't be propagated to the user's browser. try/catch :
      try
      {
        // code that might generate an exception
      }
      catch (e)
      {
        // code that executes if an exception was thrown in the try block
        // (exception details are available through the e parameter)
      }

      You place any code that might generate errors inside the try block. If an error happens, the execution is passed immediately to the catch block. If no error happens inside the try block, then the code in thecatch block never executes.Run-time exceptions propagate from the point they were raised, up through the call stack of your program. The call stack is the list of methods that are being executed. So if a function A() calls a functionB() which at its turn calls a function called C(), then the call stack will be formed of these three methods. If an exception happens in C(), you can handle it using a try/catch block right there. If the exception isn't caught and handled in C(), it propagates, to B(), and so on. The final layer is the web browser. If your code generates an exception that you don't handle, the exception will end up getting caught by the web browser, which may display an unpleasant error message to your visitor.

    6. Another technique is to use a JavaScript feature called object detection. This feature allows you to check whether a particular object is supported by the browser, and works like this:
      if (window.XMLHttpRequest)
      {
        xmlHttp = new XMLHttpRequest();
      }
    7. n
    8. n
    9. n
    10. n
    11. n
    12. n
  • 相关阅读:
    Ubuntu18.04 安装Redis
    前端周刊,给前端同学准备的每周1小时阅读清单
    如何开发第三方小程序组件
    奋斗路上的安全边际,程序员保险配置指南
    为了知道儿子到底胖不胖,做了一个婴幼儿成长标准计算小程序
    Vue组织架构图组件
    最好用的jQuery-Ajax缓存插件
    Vue-Access-Control:前端用户权限控制解决方案
    RESTful学习及应用
    纯前端实现人脸识别-提取-合成
  • 原文地址:https://www.cnblogs.com/wade-case/p/3156413.html
Copyright © 2011-2022 走看看