ajax简介
1.创建XMLHttpRequest对象:
function newXMLHttpRequest() {
var requestObj;
if (window.XMLHttpRequest) {
// Non IE
requestObj = new ActiveObj('XMLHttpRequest');
} else {
//IE
requestObj = new ActiveObj('Microsoft.XMLHTTP');
}
return requestObj;
}
2.设置在请求过程中状态发生变化时要调用的函数,当然这个过程中它会被调用好几次:
var xmlRequest;
xmlRequest = newXMLHttpRequest();
xml.onreadystatechange = callbackhandler;
在发送过程中会有5种状态的转换:
状态数字码描述
0
未初始化状态。这是在开始操作之前readyState域最初的值
1
正在加载状态。它意味着open()方法被调用了,但还没有调用send()方法
2
已经加载状态。它意味着send()方法被调用,并且对象已经完成请求了,但是还没有接收到数据,此时头信息和状态是可用的
3
正在接收(或者说交互)状态。此时数据正在接收中,reponseText中存储着到目前为止接受到的数据
4
交互完成状态。请求已经完成并且已经接收到完整的响应
3.调用open方法告诉对象想调用什么URL来处理这次请求,它有三个参数,第一个参数是用来指定使用什么http方法(post、get等),第二个就是要执行的url,第三个参数是boolean类型,告诉对象是否需要异步执行调用,一般都是true:
XMLRequest.open("post", url, true);
4.调用send()来设置要发送到服务器端的数据,注意此时发送的数据最好使用encodeURIComponent() 方法进行编码,否则像空格这类的字符就会出现乱码:
XMLRequest.send('value1=' + encodeURIComponent(value1) + '&value2=' + encodeURIComponent(value2));
在状态转换时处理的函数中只要判断readyState = 4时即可,当然这个函数可以进一步分解,在里面包装XML的验证等操作。自己慢慢去研究吧。
相关文档:
http://www.ajaxlines.com/ajax/stuff/article/using_google_is_ajax_search_api_with_java.php
I was rather depressed over a year ago when Google deprecated their SOAP Search API with their AJAX Search API. Essentially Google was saying that they didn want anyone programmatically accessing Google search ......
<html>
<head>
<title></title>
</head>
<mce:script type="text/javascript"><!--
var xmlHttp = false;
try{
xmlHttp = new XMLHttpRequest();
}catch(trymicrosoft){
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHT ......
function send_request(callback, urladdress, isReturnData){
var xmlhttp = getXMLHttpRequest();
xmlhttp.onreadystatechange = function(){
&nb ......
网页中出现“'sys' 未定义
”或“'Sys' is undefined”的错误。
此时我们要做的是在 web.config
中 <system.web> 一节下面添加类似
如下内容:
<httpHandlers>
<add
verb="GET,HEAD" path="ScriptResource.a ......
xml数据:
writer.write("<root>" +
"<name>a</name>" +
"<name>b</name>"+
"</root>");
前台解析:
request.onreadystatechange=function(){
if(request.readyState==4){
var x=requ ......