`

Ajax基础

 
阅读更多
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }


open(method,url,async);
send(string);

Method Description
open(method,url,async) Specifies the type of request, the URL, and if the request should be handled asynchronously or not.

method: the type of request: GET or POST
url: the location of the file on the server
async: true (asynchronous) or false (synchronous)
send(string) Sends the request off to the server.

string: Only used for POST requests


GET or POST?

GET is simpler and faster than POST, and can be used in most cases.

However, always use POST requests when:

A cached file is not an option (update a file or database on the server)
Sending a large amount of data to the server (POST has no size limitations)
Sending user input (which can contain unknown characters), POST is more robust and secure than GET


Method Description
setRequestHeader(header,value) Adds HTTP headers to the request.

header: specifies the header name
value: specifies the header value

xmlhttp.open("POST","ajax_test.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");



Server Response

To get the response from a server, use the responseText or responseXML property of the XMLHttpRequest object.

Property Description
responseText get the response data as a string
responseXML get the response data as XML data



The responseText Property

If the response from the server is not XML, use the responseText property.

The responseText property returns the response as a string, and you can use it accordingly:
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;


The responseXML Property
xmlDoc=xmlhttp.responseXML;
txt="";
x=xmlDoc.getElementsByTagName("ARTIST");
for (i=0;i<x.length;i++)
  {
  txt=txt + x[i].childNodes[0].nodeValue + "<br />";
  }
document.getElementById("myDiv").innerHTML=txt;


The onreadystatechange event



onreadystatechange Stores a function (or the name of a function) to be called automatically each time the readyState property changes

readyState Holds the status of the XMLHttpRequest. Changes from 0 to 4:
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready

status
200: "OK"
404: Page not found
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }



分享到:
评论

相关推荐

    csdn 图书 Ajax基础教程.chm

    《Ajax基础教程.chm》 Ajax技术可以提供高度交互的Web应用,给予用户更丰富的页面浏览体验。本书重点介绍Ajax及相关的工具和技术,主要内容包括XMLHttpRequest对象及其属性和方法、发送请求和处理响应、构建完备的...

    ajax 基础教程源代码

    ajax 基础教程源代码ajax 基础教程源代码ajax 基础教程源代码ajax 基础教程源代码ajax 基础教程源代码ajax 基础教程源代码ajax 基础教程源代码ajax 基础教程源代码ajax 基础教程源代码ajax 基础教程源代码ajax 基础...

    精通Ajax 基础概念.核心技术与典型案例

    精通Ajax 基础 教程附带源代码及其案例

    AJAX基础.pdf

    AJAX基础.pdf AJAX基础.pdf AJAX基础.pdf

    ajax 基础

    ajax的基础知识 我看过还不错

    Ajax 基础教程中文版.pdf

    Ajax 基础教程中文版.pdf

    ajax 基础教程

    ajax 基础教程ajax 基础教程ajax 基础教程ajax 基础教程ajax 基础教程ajax 基础教程ajax 基础教程ajax 基础教程ajax 基础教程ajax 基础教程ajax 基础教程ajax 基础教程ajax 基础教程ajax 基础教程ajax 基础教程ajax ...

    Ajax基础教程(扫描版)

    原书名:Foundations of Ajax 原出版社: Apress 作者: (美)Ryan Asleson,Nathaniel T.Schutta 译者: 金灵 等 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ISBN:7115144818 上架时间:2006-2-14 ...

    Ajax基础教程(亚马逊计算机榜首图书,国内第1本Ajax图书) [第一部分 共两部分]

    本书重点介绍Ajax及相关的工具和技术,主要内容包括XMLHttpRequest对象及其属性和方法、发送请求和处理响应、构建完备的Ajax开发工具、使用JsUnit测试JavaScript、分析JavaScript调试工具和技术,以及Ajax开发模式和...

    AJax基础教程.pdf

    书名:Ajax基础教程 作者:(美)阿斯利森 舒塔

    ajax基础教程(pdf)(三)

    ajax基础教程(pdf)完整版本,希望对大家有所帮助

    ajax基础教程ajax基础教程ajax基础教程

    ajax基础教程ajax基础教程ajax基础教程ajax基础教程ajax基础教程aax基础教程ajax基础教程ajax基础教程ajax础教程ajax基础教程aja基础教程ajax基础教程ajax基础教程ajax基础教程ajax基础教程aax基础教程ajax基础教程

    Ajax基础教程中文版及源代码 part1

    堪称经典由于比较大,压缩成5个压缩卷,大家可以搜索下载,然后解压第一个卷,其它的就会自动解压 文件名分别是 Ajax基础教程中文版及源代码 part1 Ajax基础教程中文版及源代码 part2 Ajax基础教程中文...

    AJAX基础教程.pdf

    PDF格式电子书 适合有一点点JS基础,刚开始接触AJAX的人

    Ajax基础入门简介

    Ajax作为一门心得技术 ,这两年被广泛应用于网络应用中,并发挥着越来越重要的作用,本文就Ajax得基础知识做一个简单介绍

    Ajax基础教程

    60M的Ajax基础教程,非常适合初学者。

    第八章 AJAX基础

    第八章 AJAX基础 第八章 AJAX基础 第八章 AJAX基础 第八章 AJAX基础

    ASP.NET AJAX基础示例

    ASP.NET AJAX基础示例.................................................................................................ASP.NET AJAX基础示例

    Ajax基础教程中文版及源代码 part5

    堪称经典由于比较大,压缩成5个压缩卷,大家可以搜索下载,然后解压第一个卷,其它的就会自动解压 文件名分别是 Ajax基础教程中文版及源代码 part1 Ajax基础教程中文版及源代码 part2 Ajax基础教程中文...

    Ajax基础教程中文版及源代码 part3

    堪称经典由于比较大,压缩成5个压缩卷,大家可以搜索下载,然后解压第一个卷,其它的就会自动解压 文件名分别是 Ajax基础教程中文版及源代码 part1 Ajax基础教程中文版及源代码 part2 Ajax基础教程中文...

Global site tag (gtag.js) - Google Analytics