简单实用的ajax脚本
文件名:ajax.js
/**
* 取得当前页面的url
* 可以在调用ajax_query前修改
**/
var url = location.href.substr(0, location.href.length-location.search.length);
/**
* 给Function对象添加一个取得函数名的方法
**/
Function.prototype.Name = function() {
var s = Function.prototype.Name.caller.toString();
return s.split(/[\r\n]+/)[0].replace(/function\s+(\w+).+/g, '$1');
}
/**
* 函数 $
* 功能 获取dhtml对象
* 参数 e 待查找的对象id或name
* 返回 成功:对象 失败:null
*/
function $(e) {
var tag = document.getElementById(e);
if(tag) return tag;
tag = document.getElementsByName(e);
if(tag.tagName == undefined) return null;
return tag;
}
/**
* 函数 ajax_query
* 功能 向服务器发送指令,并处理返回数据
* 参数
* method 服务器端方法名
* tag 接受返回数据的dhml对象名,缺省时由服务器决定处理方式
* 其他 向服务器端传递的其他参数,可缺省
* 说明 虽然Msxml2.XMLHTTP有着比Microsoft.XMLHTTP更优秀的性能,但是在配置较低的
* 环境下并不能正常工作。当确认能够正常工作时,再删去注释
**/
function ajax_query(method, tag) {
//尝试创建XMLHTTP对象
var xmlhttp;
//// try {
//// xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
//// }catch(e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (oc) {
xmlhttp = null;
}
//// }
if(!xmlhttp && typeof XMLHttpRequest != "undefined") {
var xmlhttp = new XMLHttpRequest(); // Mozilla, Safari, ...
}
if(! xmlhttp) {
alert('错误! 缺少连接组件');
return;
}
if(arguments.length == 0) {
alert(Function.Name()+'至少需要一个方法名');
return;
}
var data =
相关文档:
<html>
<body>
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Inte ......
页面代码:
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManage ......
在《Pragmatic Ajax A Web 2.0 Primer 》中偶然看到对readyStae状态的介绍,感觉这个介绍很实在,摘译如下:
0: (Uninitialized) the send( ) method has not yet been invoked.
1: (Loading) the send( ) method has been invoked, request in progress.
2: (Loaded) the send( ) method has completed, entire respons ......
由于安全问题的考虑,Ajax(xmlhttprequest)默认是不支持跨域调用的。比如在www.cadal.com去请求www.test.cadal.com的数据,都是不行的。
解决方案有很多,总结如下:
参考:
1.利用<script>标签
Difficult to know when the content is available, no standard methodology, can be considered a "security risk" ......
摘自:http://www.ibm.com/developerworks/library/wa-aj-jsonp1/
AJAX Same-Origin Policy(SOP) limitation:
AJAX prevents cross-domail invokation, there are several ways to by pass this limitation.
1. write a proxy on the server side. The SOP limitation only exists only on the javascript si ......