Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the XMLHttpRequest application function: downloadUrl ()

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

本篇内容主要讲解"如何理解XMLHttpRequest应用函数:downloadUrl()",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何理解XMLHttpRequest应用函数:downloadUrl()"吧!

downloadUrl(url, callback, data);

参数说明:

url不用说了;

callback是回调函数,函数调用的时候会有两个参数:data, responseCode,data就是responseText,responseCode就是status;

data是要post的数据,get方式时此参数可省略。

用法一:直接把回调函输写在参数中

downloadUrl('http://www.ugia.cn/wp-data/test.htm', function (data, responseCode) {

alert(data); // 这里处理返回的数据

});

用法二:先定义回调函数,然后传入

function test(data, responseCode) {

alert(data); // 这里处理返回的数据

}

downloadUrl('http://www.ugia.cn/wp-data/test.htm', test);

源代码:

代码如下:

/**

* download url lite

*

* @author: legend(legendsky@hotmail.com)

* @link: http://www.ugia.cn/?p=122

* @version: 1.0

*

* @param string url

* @param string callback 回调函数

* @param string data post数据

*

* @return void

*/

function downloadUrl(url, callback, data)

{

// init

url += url.indexOf("?") > 0 ? "&" : "?";

url += "random_download_url=" + Math.random();

if (typeof data == 'undefined')

{

var data = null;

}

method = data ? 'POST' : 'GET';

// create XMLHttpRequest object

if (window.XMLHttpRequest)

{

var objXMLHttpRequest = new XMLHttpRequest();

}

else

{

var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];

for(var n = 0; n < MSXML.length; n ++)

{

try

{

var objXMLHttpRequest = new ActiveXObject(MSXML[n]);

break;

}

catch(e)

{

}

}

}

// send request

with(objXMLHttpRequest)

{

//setTimeouts(30*1000,30*1000,30*1000,30*60*1000);

try

{

open(method, url, true);

if (method == 'POST')

{

setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

}

send(data);

}

catch(e)

{

alert(e);

}

// on ready

onreadystatechange = function()

{

if (objXMLHttpRequest.readyState == 4)

{

callback(objXMLHttpRequest.responseText, objXMLHttpRequest.status);

delete(objXMLHttpRequest);

}

}

}

}

到此,相信大家对"如何理解XMLHttpRequest应用函数:downloadUrl()"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report