zoukankan      html  css  js  c++  java
  • the XMLHttpRequest Object

        Ajax的核心是XMLHttpRequest对象。XMLHttpRequest对象允许异步发送请求并且以文本的形式返回结果。发送什么样的请求(what),在服务器端怎样处理这个请求(how),返回什么都由我们自己决定(what to return)。在Explorer 7,Firefox,Safari,Opera中,XMLHttpRequest被视为一个本地的Javascript 对象,但是在IE7前,它被看做是ActiveX 对象。所以考虑到兼容性,可以这样创建XMLHttpRequest对象。

       var xmlRequest;

    try{

       xmlRequest=new XMLHttpRequest();

    }

    catch(err)

    {

     xmlRequest=new ActiveXObject("Microsoft.XMLHTTP");

    }

    Sending a Request

    XMLHttpRequest两个主要的方法:open(),send().

    xmlRequest.open("GET",myurl); -----用来定义要发送的请求。

    xmlRequest.send(null); --------------用来激活请求。(fires off the request)

    how to handle the response?

    the secret is to attach an event handler using the onreadystatechange property.this property points to a client-side javascript function that is called when the request is finished and the data is available.Of course ,you need to attacth teh event handler before you call the send() method to start the request.

    XMLHttpRequest对象的ReadyState属性值列表。

      ReadyState取值 描述 

      0 描述一种"未初始化"状态;此时,已经创建一个XMLHttpRequest对象,但是还没有初始化。

      1 描述一种"发送"状态;此时,代码已经调用了XMLHttpRequest open()方法并且XMLHttpRequest已经准备好把一个请求发送到服务器。

      2 描述一种"发送"状态;此时,已经通过send()方法把一个请求发送到服务器端,但是还没有收到一个响应。

      3 描述一种"正在接收"状态;此时,已经接收到HTTP响应头部信息,但是消息体部分还没有完全接收结束。 

      4 描述一种"已加载"状态;此时,响应已经被完全接收。

  • 相关阅读:
    12.Docker网络类型
    博客迁移
    java注解
    IO多路复用技术(multiplexing)
    Java 中extends与implements使用方法
    初识autoconf
    初识swoole
    简单配置nginx使之支持pathinfo
    vue-cli 脚手架 安装过程
    PHP阻止页面后退如何用PHP实现禁用浏览器的后退,使后退的页面失效或链接到别的地方?使用php禁止浏览器缓存?
  • 原文地址:https://www.cnblogs.com/xuezhi/p/3431860.html
Copyright © 2011-2022 走看看