方法1:
var xmlhttp_request = new ActiveXObject( "Microsoft.XMLHTTP" );
function IDRequest()
{
xmlhttp_request=getXMLRequester();
xmlhttp_request.onreadystatechange = doContents;
xmlhttp_request.open('GET', 'http://www.163.com', true);
xmlhttp_request.send(null);
}
function doContents()
{
}
方法2:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script language="javascript" type="text/javascript">
function showEmployee(firstName, lastName, title)
{
var request = new Sys.Net.WebRequest();
request.set_url('GetEmployee.ashx');
request.set_httpVerb("POST");
request.add_completed(onGetEmployeeComplete);
var requestBody = String.format(
"firstName={0}&lastName={1}&title={2}",
encodeURIComponent(firstName),
encodeURIComponent(lastName),
encodeURIComponent(title));
request.set_body(requestBody);
request.invoke();
}
function onGetEmployeeComplete(response)
{
if (response.get_responseAvailable())
{
var employee = response.get_object();
alert(String.format(
"Hello I'm {0} {1}, my title is '{2}'",
employee.FirstName,
employee.LastName,
employee.Title));
}
}
</script>
问题1:这两种方法哪个更好?大家都常用哪个方法?
问题2:还有没有其它方法?
var xmlhttp_request = new ActiveXObject( "Microsoft.XMLHTTP" );
function IDRequest()
{
xmlhttp_request=getXMLRequester();
xmlhttp_request.onreadystatechange = doContents;
xmlhttp_request.open('GET', 'http://www.163.com', true);
xmlhttp_request.send(null);
}
function doContents()
{
}
方法2:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script language="javascript" type="text/javascript">
function showEmployee(firstName, lastName, title)
{
var request = new Sys.Net.WebRequest();
request.set_url('GetEmployee.ashx');
request.set_httpVerb("POST");
request.add_completed(onGetEmployeeComplete);
var requestBody = String.format(
"firstName={0}&lastName={1}&title={2}",
encodeURIComponent(firstName),
encodeURIComponent(lastName),
encodeURIComponent(title));
request.set_body(requestBody);
request.invoke();
}
function onGetEmployeeComplete(response)
{
if (response.get_responseAvailable())
{
var employee = response.get_object();
alert(String.format(
"Hello I'm {0} {1}, my title is '{2}'",
employee.FirstName,
employee.LastName,
employee.Title));
}
}
</script>
问题1:这两种方法哪个更好?大家都常用哪个方法?
问题2:还有没有其它方法?
</asp:ScriptManager>,说明这个是aspnet的jaxa库,前一个则是不要使用jaxa lib的
帖下我平时常用的方法..
var ajaxReq = new Object();
//0 – 尚未初始化
//1 – 加载(XMLHttpRequest初始化结束)
//2 - 加载结束(XMLHttpRequest从服务器上获得一个回应)
//3 - 交互(当前XMLHttpRequest对象和服务器连接中)
//4 - 结束(当前XMLHttpRequest被告知它已经完成了所有任务并结束运行)ajaxReq.READY_STATE_UNINITIALIZED = 0;
ajaxReq.READY_STATE_LOADING = 1;
ajaxReq.READY_STATE_LOADED = 2;
ajaxReq.READY_STATE_INTERACTIVE = 3;
ajaxReq.READY_STATE_COMPLETE = 4;//默认构造涵数 (url地址 onload加载处理涵数 onerror错误处理涵数)
//javascript虽然也有面向对象的概念但是没有内建继承的概念. 所以所谓的对象实例其实是在运行时就把成员字段和方法与自己绑在了一起直接使用
ajaxReq.ContentLoader = function(url,onload,onerror){
this.url = url
this.req = null;
this.onload = onload;
this.onerror = (onerror)?onerror:this.defaultError;
this.loadXMLDoc(url);
}
//在声明构造涵数后我们才可以使用prototype
//prototype的所有属性和方法会附加到结果对象上
//也就是面向对象里里的类的实例上.
//关键字this确定为运行时对象的实例
//还可以通过prototype扩展内建类 比如一些原有的类型 比如Array
ajaxReq.ContentLoader.prototype={
loadXMLDoc:function(url){
if (window.XMLHttpRequest)
{
this.req = new XMLHttpRequest(); //检测浏览器版本 Mozila
}
else if (window.ActiveXObject)
{
this.req = new ActiveXObject("Microsoft.XMLHTTP"); //检测浏览器版本 IE
}
if (this.req) //执行异步请求的处理
{
try
{
var loader = this;
this.req.onreadystatechange = function(){
loader.onReadyState.call(loader);
}
this.req.open('GET',url,true);
this.req.send(null);
}
catch(err)
{
this.onerror.call(this);
}
}
},
onReadyState:function(){
var req = this.req;
var ready = req.readyState;
if (ready == ajaxReq.READY_STATE_COMPLETE)
{
var httpStatus = req.status;
if (httpStatus==200 || httpStatus==0)
{
this.onload.call(this);
}
else
{
this.onerror.call(this);
}
}
},
//此处为报错处理
defaultError:function(){
alert("error fetching data!");
}
}
如果想用使用可以把它在页面进行连接..
然后用类似面向对象的概念使用它var asyObj = new ajaxReq('url',回调涵数,错误处理涵数)