亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

使用 XMLHTTPRequest 編程原文章和注意事項

2019-11-18 14:25:19
字體:
供稿:網(wǎng)友

  用 xmlHTTPRequest 編程原文章和注重事項
Client Side Validation Using the xmlhttpRequest Object
By Jonathan Zufi Rating: 4.3 out of 5
Rate this article


print this article

email this article to a colleague

suggest an article
IntrodUCtion

As an avid reader of 15 seconds, I´ve been eXPosed to many articles on form validation. Form validation should not only address checking fields for content and structure, it should also handle real-time lookups on data. This article shows how Microsoft´s XMLHTTPRequest component can give your users a cleaner and more efficient Web experience by improving form validation.


Programming for HTTP


There have always been several ways of making HTTP calls on the Win32 platform. Visual Basic and C++ developers could leverage the WinInet library, and Visual Basic programmers could utilize the Internet Controls that shipped with Visual Basic 6.0. However, asp developers have had a bit more work getting access to this functionality, since they would need to wrap it in a custom component for elegant interaction with ASP code. ´Raw´ scripting makes the task difficult.

Most people assume HTTP is a protocol reserved for browsers to communicate with Web servers. Remember, HTTP is just a protocol, and a powerful one at that. HTTP can be used by any application to talk to another application or component; it doesn´t have to be a browser or a Web server. As Web developers, we are all familiar with the benefits of using the HTTP protocol. It works well across firewalls, is based on Internet standards, etc.

Microsoft included the XMLHTTPRequest component in its XML toolkit so XML documents could be passed around over the Internet using the standard HTTP protocol. While all the documentation speaks of XML interchange over HTTP, the nice thing about this component is that you don´t have to deal with XML at all to leverage its power. Only a few lines of code are necessary to make a HTTP call and capture the results. This makes the XMLHTTPRequest component an extremely powerful tool in every Web developer´s arsenal, especially ASP developers.


XMLHTTPRequest and XMLHTTP


The XMLHTTPRequest component is part of MSXML, which ships with Internet Explorer 5.0 and higher. This makes it an even more attractive tool. The core object inside XMLHTTPRequest is the XMLHTTP object. There are different versions of the XMLHTTP object, since it´s been included in every version of the Microsoft MSXML package. For a good overview of the latest versions and installation issues, see the Microsoft knowledge base article Q278969.

The XMLHTTP object provides all of your Web browser´s communication functionality with just a handful of methods and properties. To use this object, create the XMLHTTP object, call the open method with the URL you want to talk to, the type of call to make (GET, POST), and then invoke the send method. The object will basically act like a browser and retrieve the data from the URL, making it available in the responseText property. You can also make synchronous or asynchronous calls. There is also a callback facility available for asynchronous calls. Very neat and very simple.


Using XMLHTTP to perform real time data validation


Say that you are capturing user registration details for your Web site, and one of the fields is ´User ID´. This User ID obviously needs to be unique across your site, so you´ll have a requirement to ensure that the User ID supplied at registration time does not already exist. If it does, you´ll need to warn the user and ask them to re-enter it.

The common way of dealing with this type of requirement is to apply the lookup logic when the form is posted. However, this can sometimes lead to what is not the best user experience. We all know how frustrating it is to continually submit forms only to find that we have forgotten a value here or there. The other downside of posting the page is that if it needs to be re-rendered with the appropriate ´please correct this problem´ messages, that´s another trip to the server, images, script and all.

The ideal way to handle this is to alert users as soon as they have entered their username as to whether or not it is unique. On a desktop app, this would be simple - put some code in the ´lost focus´ event of the text box. javascript and the XMLHTTP object can provide the same level of interaction.

Let´s walk through an example, registering with a fictitious company.



The Html for the User ID field looks like this:


<input type="text" name="UserID" onblur="validateuserid(this.value);">


The ´onblur´ event will fire our validation routine when the user tabs out of the User ID textbox. (Note: if you´re not a fan of focus driven validation, you could move this to the onclick event of the Register button).
Examine the Javascript that performs the validation:


<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
function validateuserid(suserid) {

document.body.style.cursor=´wait´;

// Create an instance of the XML HTTP Request object
var oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );

// Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
var sURL = "http://mysite/mypath/validateuser.asp?username=" + suserid
oXMLHTTP.open( "POST", sURL, false );

// Execute the request
oXMLHTTP.send();

if (oXMLHTTP.responseText == "exists")
alert("Sorry - the User ID " + suserid + " already exists.");

document.body.style.cursor=´auto´;
}
</SCRIPT>


Let´s go through the script block in detail to see what its doing:

document.body.style.cursor=´wait´;


The script is going to make a HTTP call, which might take a second or two, so this line changes the mouse to an hourglass to give the user some feedback. From the user experience point of view, there are prettier things you can do with the UI other than changing the mouse pointer. Javascript and DHTML give you everything you need. In fact, there is a much more elegant way to handle this, which we´ll examine later on.

// Create an instance of the XML HTTP Request object
var oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );


This creates an instance of the XMLHTTP object.

// Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
var sURL = "http://mysite/mypath/validateuser.asp?userid=" + suserid
oXMLHTTP.open( "GET", sURL, false );


The ´open´ method prepares the request that we are about to make. The first parameter defines the connection to use. In this case, we´ll use the GET method. ´open´ supports other methods such as PUT and POST. (POST is VERY powerful, because as implied, you can programmatically post forms as well). The next parameter specifies the URL that we are connecting to. Since we are running this script on the client, this should be an absolute URL. Here we construct a call to a custom ASP page which will tell us whether or not the User ID exists (more on this in a moment).
The third parameter is used to tell the object whether or not to make the call asynchronously. I´ve set this to False, indicating that we want to wait until the call returns before continuing.

There are two other optional parameters, username and passWord. These allow a connection to a site that requires username/password authentication.

Now that we have prepared the request, we just need to send it.


// Execute the request
objXMLReq.send();


At this point the object will actually go out over the Internet and bring back the contents of the target page. Now we check those contents to see what happened:

if (objXMLReq.responseText == "exists")
alert("Sorry - the User ID " + suserid + " already exists.");


If the User ID has already been assigned to another user, the page returns the word ´exists´ to indicate that the user id already exists in the database. (You could use any protocol you like - for example, the ASP page could have returned a number.) Finally we reset the cursor.

document.body.style.cursor=´auto´;


Now let´s look at the ASP page that the script is communicating with to do the lookup:

<%
Dim objConn, objRS, sUserID

´Capture the username that we need to lookup, making sure its prepared for
´our forthcoming SQL query
sUserID = Replace(Trim(Request.QueryString("userid")),"´","")

´Fire up ADO - ask the database whether or not the user idexists
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open CONNECTIONSTRING
sSQL = "select userid from usertable where userid = ´" + sUserID + "´"
Set objRS = objConn.Execute(sSQL)
If Not objRS.EOF Then Response.Write "exists"

´Clean up
objRS.Close
objConn.Close
Set objRS = Nothing
Set objConn = Nothing
%>


This is a straightforward piece of ASP code that writes out the word ´exists´ if the passed-in User ID was found in the database. If you ran this page on its own, the browser would be blank if the User ID did not exist -- the page would just say the word ´exists´ if it did exist. This means that you can test your ASP page in isolation before you integrate it back with your client script.

Dealing with timeouts


The first issue that has probably crossed your mind is ´what if the remote site is down - how do I handle timeouts and errors?´ While trapping an error from the ´send´ method is feasible, this doesn´t fix the time the user would have to wait while the call is being made.

Recall the third parameter on the ´open´ method of the XMLHTTP object. We set it to ´false´. Setting it to ´true´ will make the call immediately, and the control will drop through to the next line, so you need to either poll or be notified (i.e. event) that the call has completed (successfully or not). Luckily, the XMLHTTP object can notify the function of your choice as it moves through its various stage of processing the HTTP request. Let´s have a look at a slightly modified version of the client side script that deals with potential timeouts:


<!-define a div that will be our pseudo progress indicator -->
<div id="divProgress">Please wait</div>

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
// Create an instance of the XML HTTP Request object
var oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );
var userid;
function validateuserid(suserid) {

// Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
userid = suserid;
var sURL = " http://mysite/mypath/validateuser.asp?userid=" + userid;
oXMLHTTP.open( "POST", sURL, false );

// Define an event handler for processing
oXMLHTTP.onreadystatechange = managestatechange;

// Execute the request
try {
oXMLHTTP.send();
}
catch (e) {
alert("Could not validate your User ID at this time.");
document.all.item("FirstName").focus;
}
}

function managestatechange() {
switch (oXMLHTTP.readyState) {

case 2, 3:
// Display a progress indicator of some kind, informing the
// user that you are checking to see if the UserID exists
document.all.item("divProgress").style.display = "block";
break;

case 4:
if (oXMLHTTP.responseText == "exists")
alert("Sorry - the User ID " + userid + " already exists.");
document.all.item("divProgress").style.display = "none";
break;
}
}

// Hide the progress indicator for now
document.all.item("divProgress").style.display = "none";

</script>


The code of interest here is line where we set the event handler:

// Define an event handler for processing
oXMLHTTP.onreadystatechange = managestatechange;


This tells the XMLHTTP object to call the ´managestatechange´ function as it the state of the HTTP request changes - from the time the object is created until the point where the request has completed. There are five different states that you can monitor:
0 (UNINITIALIZED) The object has been created, but not initialized (open method has not been called).
(1) LOADING The object has been created, but the send method has not been called.
(2) LOADED The send method has been called and the status and headers are available, but the response is not yet available.
(3) INTERACTIVE Some data has been received. You can call responseBody and responseText to get the current partial results.
(4) COMPLETED All the data has been received, and the complete data is available in responseBody and responseText.

- from MSDN

In the updated script, I also make it a little more robust by adding some error handling. After all, the remote site may be down or the page may not exist:


// Execute the request
try {
oXMLHTTP.send();
}
catch (e) {
alert("Could not validate your User ID at this time.");
document.all.item("FirstName").focus;
}


When the code in the callback function is finished, the control will return to the calling routine, and if a problem occurred, you can cater for it. Note that I set the focus to another field. This is important, otherwise the user would never be able to move out of the User ID field.
It is useful to note at this stage that there is another component called the ServerXMLHTTP object - see the Microsoft knowledge base article Q290761 for details about the differences between these two components. ServerXMLHTTP, as the name implies, is more suited for HTTP connectivity from the server end and is a little more elegant when dealing with timeouts. This is useful to know if you want to do some bigger and better things. This article was focused on client side validation so we focused on the XMLHTTP object since it was actually designed and optimized to be a client side component.

I hope this article has shown you how powerful the XMLHTTPRequest object from Microsoft is. Check out the MSDN Web site for more examples.


About the Author


Jonathan Zufi is the founder of UDU World (http://www.uduworld.com), a company that sells solutions based on their ActiveInbox technology. ActiveInbox is a back end information and content delivery platform that provides information access over email and mobile text messaging.

Jonathan has been developing software for over ten years. He holds an Honour´s degree in Robotics and Digital Technology from Monash University (Melbourne, Australia). He was recently awarded the Pearcey Award from the Australia Computer Society, which recognizes innovative and pioneering achievement and contribution to research and development in Information and Computer Technology across Australia. Jonathan may be reached at jonathanzufi@UDUworld.com

注重事項:
XMLHTTP提交過時返回的是亂碼----肯定許多同仁發(fā)現(xiàn)了!
解決:
我們在使用過程中從來不用任何非凡處理,只需在服務(wù)端asp上寫上
Response.ContentType = "text/xml"
Response.CharSet = "UTF-8" ´unicode
不管怎樣用,一定不會出亂碼。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
日韩一区二区电影在线观看| 国产黄色片av| 日韩精品无码一区二区三区免费| 国产高清一区日本| 一本久道综合色婷婷五月| 91美女在线观看| 亚洲成人精品视频在线观看| 五月激情丁香婷婷| 一区二区在线视频观看| 日韩—二三区免费观看av| 欧美性猛交xxxx乱大交丰满| 精品国产1区| 亚洲天堂2020| 日韩一级视频免费观看在线| 中国老熟女重囗味hdxx| 成人资源视频网站免费| 精品人妻伦九区久久aaa片| yourporn在线观看视频| 男人女人黄一级| 精品久久久99| 丝袜理论片在线观看| 另类成人小视频在线| 国内老熟妇对白hdxxxx| 中文字幕一区二区三区在线观看| 国产精品视频精品视频| 天堂资源在线中文精品| 亚洲欧美视频在线观看视频| 精品三级久久| 99热这里有精品| 亚洲精品久久久久国产| 欧美性xxxx交| 成人啪啪免费看| 一区二区三区自拍| 日韩电影大全免费观看2023年上| 欧美黄色直播| 我要看一级黄色录像| 91欧美在线视频| 日韩大尺度视频| 少妇毛片一区二区三区| 黄色软件视频在线观看| 岛国大片在线免费观看| 欧美91在线| 免费在线中文字幕| 精品一区二区精品| 成人污网站在线观看| 久久久91精品国产| 国产乱码精品一区二区三区四区| 久久久av免费| 亚洲精品白浆高清| 色姑娘资源站| 亚州国产精品久久久| 亚洲91精品| 九九热视频在线观看| 日韩精品亚洲aⅴ在线影院| 亚洲综合一区二区| 日韩精品大片| 成人拍拍拍免费视频网站| 亚洲精品一二区| 欧美日本不卡高清| 亚洲字幕av一区二区三区四区| 国产色综合天天综合网| 亚洲h色精品| 色婷婷精品大在线视频| 9i看片成人免费高清| 激情综合网最新| 国产一区深夜福利| 在线免费黄色网| 中国女人内谢69xxx视频| 玉足女爽爽91| 成人激情免费在线| 欧美日韩大片一区二区三区| www.亚洲天堂.com| 午夜精品在线观看| 一本久道久久综合多人| 中文字幕电影av| 国产精品日韩精品中文字幕| 最新在线观看av网站| 999福利在线视频| 视频一区日韩精品| 国产精品996| 亚州视频一区二区三区| 91浏览器在线视频| 国产高清亚洲一区| 亚洲人高潮女人毛茸茸| 日韩美女一区二区三区在线观看| 国产精品久久久久一区二区国产| 欧美性大战久久久久久久蜜臀| 高清福利在线观看| 伊人午夜电影| 一区二区视频免费在线观看| 国产美女视频一区| 先锋影音欧美性受| 日韩精品视频中文在线观看| 成人精品一区二区三区免费| 国产伦精品一区二区三| 国产乱色在线观看| 国产精选一区| 日韩精品影视| 夜色视频网站| gogogo免费高清日本写真| 日韩精品视频播放| 亚洲精品美女网站| 天天夜碰日日摸日日澡性色av| 一区二区三区少妇| 精品理论电影在线| 成人高清在线视频| 精品视频高清无人区区二区三区| 三级成人黄色影院| 拍拍拍在线观看视频免费| 日韩综合另类| 亚洲综合视频网| 国产精品入口免费软件| 久久久久久欧美精品色一二三四| 国产精品国产a级| 456亚洲精品成人影院| wwwxxxx欧美| 日韩美女视频中文字幕| 国产精品久久亚洲不卡| 亚洲久久久久久久| 欧美高清在线播放| 亚洲h动漫在线| 妖精视频成人观看www| 免费观看成人高潮| 日韩免费电影一区二区| 妞干网在线免费视频| 99久久久国产精品| 天堂www在线а√天堂| 日韩av在线免费观看不卡| aaa一区二区三区| 啦啦啦啦免费高清视频在线观看1| 日本高清视频一区二区三区| 一道本成人在线| 99成人在线观看| www.麻豆av.com| 中文字幕天堂在线| av在线导航| 丝袜连裤袜欧美激情日韩| 热99精品视频| 91论坛在线播放| 国产亚洲一区二区三区在线观看| 国产精品久久久久久中文字| 亚洲一级一区| 97色伦亚洲国产| 精品无人区一区二区三区竹菊| 日韩国产美国| 亚洲国产精品成人综合| 欧美视频综合| 成人久久精品| 欧美成人r级一区二区三区| 91麻豆国产精品久久| 麻豆国产高清在线播放| 4438成人网| 五月天婷婷综合网| 日韩欧美国产免费播放| 亚洲第一页在线播放| 男人的天堂在线视频| 9191国语精品高清在线| 妞干网在线观看视频| 四季av中文字幕| 欧美视频在线免费看| 鲁片一区二区三区| 亚洲国产精彩中文乱码av| 日韩av成人在线| 欧美aaa免费| 免费成人看片网址| 成人国产免费视频| 国产成人精品123区免费视频| 亚洲成人影院在线观看| 国产免费麻豆视频| 91精品视频播放| 91福利国产成人精品照片| 久久99热这里只有精品国产| 亚洲欧美在线综合| 色婷婷狠狠18禁久久| 中文精品视频| 美女又爽又黄免费动漫| 日韩三区免费| 少妇久久久久久久久久| 精品国产乱码久久久久久蜜臀网站| 在线观看日本一区| 少妇精品视频一区二区| 亚洲第一中文字幕在线观看| 国产精品嫩草影院在线看| 精品国产一区二区三区四区精华| 久久青草欧美一区二区三区| 99免费精品在线观看| 91久久黄色| 欧美日韩成人激情| 久久色在线视频| 日韩一级在线观看| www污网站在线观看| 欧美日韩精品久久久| 97人妻精品一区二区三区免费| 黄污视频在线观看| 天堂av在线网站| 亚洲一级黄色av| 国产精品久久久久久一区二区| 一级特黄性色生活片| 欧美放荡的少妇| 国产一区二区波多野结衣| 成人软件网18免费视频| 国产精品6699| 蜜桃欧美视频| 免费看黄色aaaaaa 片| 经典一区二区| 免费毛片a线观看| 99久久精品免费看| 欧美特黄一级视频| 久久综合导航| 国产女主播视频一区二区| 国产精品videossex| 岛国精品在线观看| 中文字幕在线观看你懂的| 九一精品在线观看| 亚洲精品久久久久久久久| 日本粉色视频在线观看| 国产精品99一区二区三| 欧美在线高清| 深夜福利视频网站| 欧美男人天堂| 午夜免费入口| 精品无人区太爽高潮在线播放| 一区在线观看免费| 久久精品国产一区二区三区不卡| 精品一区二区电影| 精品人妻一区二区三区蜜桃| 日韩精品在线观看一区| 欧美xnxx| 五月婷婷色丁香| 欧美电影免费观看完整版| av影院在线播放| 国产亚洲网站| 欧美日韩高清一区二区不卡| 欧美午夜片在线免费观看| 日本午夜人人精品| 国产色在线播放| 蜜桃久久久久| 欧美成人短视频| 阿v天堂2018| 欧美极品欧美精品欧美图片| 欧美日韩精品一区二区视频| 老汉色老汉首页av亚洲| 色婷婷国产精品| 91色在线porny| 色综合视频一区中文字幕| 在线视频一区二区三区四区| 亚洲免费在线观看视频| 人人干人人爱人人爱| 午夜视频福利在线| 91tv精品福利国产在线观看| 色综合天天视频在线观看| 天天av天天翘天天综合网| 制服丝袜中文字幕在线观看| 国产91高潮流白浆在线麻豆| 91亚洲国产成人精品一区二三| 中文字幕一区二区三区四区免费看| 欧美二区在线看| 精品日韩在线观看| 91在线免费看网站| 国产偷激情在线| 欧美大香线蕉线伊人久久国产精品| 欧美日本国产视频| 午夜影院观看视频免费| 男人天堂欧美日韩| 亚洲japanese制服美女| 秋霞午夜理伦电影在线观看| 国产成人在线观看免费网站| 国产一区红桃视频| 永久免费在线| 尤物av无码色av无码| 久久蜜桃香蕉精品一区二区三区| 韩国主播福利视频一区二区三区| 日韩少妇一区二区| 亚洲欧美自拍偷拍色图| 91大神福利视频在线| 欧美国产精品一区二区三区| 亚洲资源在线观看| 欧美videos大乳护士334| 日本在线一区| 午夜精品久久久久99蜜桃最新版| 久久久久国色av免费观看性色| 老司机精品久久| 中文字幕免费不卡在线| abab456成人免费网址| 日韩精品一区二区三区中文精品| 天堂成人国产精品一区| 欧美在线观看网址综合| youjizz在线视频| 欧美韩国理论所午夜片917电影| 国产精品99一区二区三| av免费不卡国产观看| 免费成人高清视频| 一区二区三区四区在线看| 最新一本之道波多野结衣| 国产精品小说在线| 国内老熟妇对白xxxxhd| 97婷婷大伊香蕉精品视频| 亚洲欧美制服综合另类| 最新成人av在线| 日韩中文字幕高清| 国产精品高潮呻吟久久久| 成人国内精品久久久久一区| 亚洲国产日韩综合一区| 激情亚洲影院在线观看| 精品无人乱码一区二区三区的优势| 国产一区二区在线影院| 在线亚洲午夜片av大片| 精品少妇一区二区三区视频免付费| 欧美日韩精品是欧美日韩精品| 一级特黄aa大片| 亚洲区 欧美区| 成人3d精品动漫精品一二三| 日本黄色小说视频| 国产精美视频| 久久久精品视频成人| 久久精品噜噜噜成人av农村| 国产一二三av| 99电影网电视剧在线观看| 在线观看一区二区三区三州| 中国色在线观看另类| 菠萝菠萝蜜在线观看| 性欧美videos另类hd| 91精品综合| 国产麻豆一级片| 性xx色xx综合久久久xx| 青草影视电视剧免费播放在线观看| 国产精品久久久久久久久男| 国产精品无码在线|