日本免费精品_最新日韩一区_亚洲视频一区在线_a在线视频观看_天天射夜夜骑_粉嫩av一区二区三区_欧美中日韩免费视频_综合图区欧美_国内精品美女在线观看_午夜精品久久久久久久男人的天堂

首頁 > 學院 > 開發設計 > 正文

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

2019-11-18 14:25:19
字體:
來源:轉載
供稿:網友

  用 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提交過時返回的是亂碼----肯定許多同仁發現了!
解決:
我們在使用過程中從來不用任何非凡處理,只需在服務端asp上寫上
Response.ContentType = "text/xml"
Response.CharSet = "UTF-8" ´unicode
不管怎樣用,一定不會出亂碼。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
日韩精品中文在线观看| 欧美日韩国产91| 免费中文字幕日韩欧美| 午夜视频在线观看一区| 99综合视频| 99这里有精品视频| 久久久综合av| 欧美色欧美亚洲高清在线视频| 成年人看的羞羞网站| 一区二区在线观| 欧美日韩国产在线观看| 中文精品在线| 亚洲乱码视频| 中文亚洲免费| 日本精品国语自产拍在线观看| 亚洲视频电影在线| 日韩不卡一二三区| 欧美 日韩 国产 在线观看| 精品久久久久久综合日本欧美| 亚洲欧美999| 一区二区视频在线| 欧美日韩国产在线观看| 亚洲欧美999| 一区二区三区久久| 日韩精品国产欧美| 天堂中文在线视频| 蜜臀91精品国产高清在线观看| 不卡在线视频| 久久av免费| 精品中文在线| 一区二区不卡视频| 国产超级va在线视频| 日韩在线观看视频一区二区| 亚洲校园欧美国产另类| 免费在线播放av| 天堂在线www天堂中文在线| 91精品999| 免费在线视频一区二区| 日韩中文字幕精品视频| 欧洲精品在线一区| 中文字幕在线观看亚洲| 亚洲 欧美 中文字幕| 日韩在线视频网| 日韩手机在线观看视频| 日本免费在线视频不卡一不卡二| 亚洲一区激情| 欧美国产小视频| 精品久久久久久无| 激情国产在线| 亚洲黄在线观看| 麻豆精品视频入口| 日韩三级一区| 国产激情久久| 亚洲国产福利视频| 免费视频久久| 国产三级精品在线观看| 精品亚洲综合| 中文字幕欧美日韩精品| 亚洲中文字幕一区| 国产手机视频一区二区| 日韩欧美国产1| 一区二区三区久久| 天天综合天天| 日本精品免费观看高清观看| 欧美在线日韩精品| 日韩在线观看一区| 欧美国产日韩综合| 91久久精品网| 成人a在线观看| 欧美日韩精品在线观看| 国产区视频在线观看| 国产高清在线观看| 国产午夜精品久久| 国产一级一片免费播放| 在线资源av| 日韩精品不卡一区二区| 亚洲国产一区自拍| 日韩视频专区| 欧美另类专区| 久久精品夜夜夜夜久久| 精品国产欧美| 亚洲第一网中文字幕| 最近免费中文字幕在线第一页| 中文字幕亚洲视频| 中文字幕在线亚洲| 亚洲一区视频在线观看视频| 欧美黄页在线免费观看| 亚洲欧美日本国产专区一区| 国产不卡视频在线| 欧美日韩精品在线| 日韩中文字幕在线一区| 欧美亚洲天堂| 国产免费久久| 中文国产字幕在线观看| 欧美久久久久久蜜桃| 日韩一级二级三级精品视频| 91精品在线观| 一级片在线免费看| 中文字幕欧美国内| 国产一区激情在线| 91精品久久久久久久91蜜桃| 你懂的亚洲视频| 精品日韩欧美在线| 免费国产h视频在线观看86| 中文字幕 欧美 日韩| 欧洲精品二区| 国产在线精品日韩| 亚洲第一页中文字幕| 国产91久久久久| 亚洲丝袜一区| 欧美不卡一二三| 国产在线欧美| 亚洲国产专区| 在线视频你懂得一区| 中文国产字幕在线观看| 黄色片免费看| 日韩在线视频免费观看高清中文| 91九色在线看| 精品视频—区二区三区免费| 亚洲校园欧美国产另类 | 精品视频1区2区3区| 日韩精品福利一区二区三区| 日韩国产在线观看一区| 色综合婷婷久久| 欧美日韩免费视频| 欧美日韩精品三区| 欧美一级免费看| 欧美国产小视频| 日韩精品 欧美| 日韩欧美成人一区二区三区| 亚洲福利精品| 中文字幕在线国产| 久久99精品国产| 中文字幕欧美日韩在线不卡| 欧美日韩午夜在线| 欧美亚洲国产日韩| 一区二区日韩视频| 91极品视频在线观看| 国产高清精品在线观看| 国产高清视频一区二区| 日韩区欧美区| 伊人www22综合色| 你懂的亚洲视频| 日韩欧中文字幕| 日韩不卡一二区| 日韩精品视频中文字幕| av中文网站| 中文字幕欧美亚洲| 香蕉视频亚洲一级| 日韩视频免费直播| 视频一区视频二区中文| 欧洲精品二区| 欧美日韩国产中文精品字幕自在自线| 日韩激情一区| 91久久精品国产| 黄色精品在线观看| 欧美日韩人人澡狠狠躁视频| 日韩字幕在线观看| 中文字幕4区| 交视频在线观看国产| 一二三区精品视频| 97久久精品| 国内不卡的二区三区中文字幕| 日韩欧美国产1| 国产不卡在线| 日韩字幕在线观看| 亚洲电影中文字幕在线观看| 国产视频1区2区| 日韩在线视频一区| 中文字幕精品在线视频| 国产黄色片大全| 欧美日韩三级在线观看| 精品人妻一区二区免费视频| 国产欧美中文在线| 91精品国产综合久久香蕉的特点| 美国av一区二区| 亚洲国产欧美久久| 日韩中文字幕在线视频播放| 亚洲乱码中文字幕| 精品视频www| 欧美日韩第一区日日骚| 91精品国产欧美日韩| 韩国v欧美v日本v亚洲| 97最新国自产拍视频在线完整在线看| 欧美在线视频第一页| 久久网站免费观看| 欧美国产亚洲一区| av影视在线看| 亚洲成年人影院在线| 欧美二三四区| 中文字幕国产欧美| 蜜桃久久久久| 久久久久久99精品| 欧美日韩一二| 国产卡1卡2卡三卡在线| 国产欧美中文在线| 亚洲一区视频在线观看视频| 精品一区二区三区中文字幕| 中文字幕在线视频网| 在线视频日韩欧美| 国产99对白在线播放| 日韩精品福利一区二区三区| 国产免费一级片| 亚洲欧美久久久| 一区在线视频观看| 在线精品日韩| 黄污视频在线看| 91色在线看| 亚洲成av人片| 91精品蜜臀在线一区尤物| 欧美日韩激情一区二区三区| 99久久精品国产成人一区二区| 欧美中日韩在线| 精品全国在线一区二区| 国产三级在线播放| aaa欧美日韩| 国产一级片播放| 日韩三级视频在线看| 精品久久在线观看| 91久久精品网| 91精品国产入口| 国产网站av| 国产三级精品网站| 国产一区成人| 日韩午夜黄色| 欧美 中文字幕| 日韩欧美亚洲区| www.精品av.com| 中文字幕亚洲区| 欧美日韩国产高清| 欧美日韩在线播放视频| 久久精品人妻一区二区三区| 国内不卡的二区三区中文字幕| 激情国产在线| 中文精品视频| 日韩欧美中文视频| 免费精品国产自产拍在| 一区二区三区视频网站| 99久久婷婷| 久久久91精品国产| 国产九九在线| 日韩欧美一二三区| 国产真实乱子伦精品视频| 视频一区视频二区中文| 一区二区三区在线播放欧美| 欧美中文字幕视频| 日韩欧美一二三区| 99精品视频99| 国产在线看一区| 中文字幕欧美人妻精品一区蜜臀 | 欧美一级搡bbbb搡bbbb| 久久精品一二三| 国产在线观看精品| 色欧美日韩亚洲| 精品调教chinesegay| 91欧美日韩| 日韩在线播放一区二区| 日韩亚洲欧美中文三级| 欧美黄页在线免费观看| 91精品国产91| 欧美一级欧美三级在线观看| 国产日韩精品久久久| a在线观看免费视频| 91精品国产综合久久久蜜臀粉嫩| 麻豆一区二区99久久久久| 91一区二区| 精品久久电影| 日韩三级精品电影久久久| 一区二区三区免费看视频 | 中文字幕人成乱码在线观看| 国产资源在线观看| 欧美日韩亚洲一区| 91精品国产综合久久精品| 国产欧美日韩综合| 欧美日韩不卡在线视频| 欧美 日韩 国产在线| 久久韩国免费视频| av一区二区高清| 欧美中文字幕精品| 激情久久99| 国产中文在线播放| 韩国av一区二区| 日本精品在线| 午夜国产福利在线观看| 欧美日韩综合高清一区二区| 精品久久香蕉国产线看观看gif| 精品久久电影| 91精品久久久久久久久久| 最新日韩中文字幕| 国产99精品| 亚洲欧洲三级| 日韩中文字幕精品视频| 亚洲欧美伊人| 日韩精品三级| 快she精品国产999| 首页国产欧美久久| 日韩精品三级| 精品久久久精品| 欧洲一级精品| 国产最顶级的黄色片在线免费观看| 日韩av在线中文字幕| 中文字幕日韩视频| 正在播放日韩精品| 日韩精品在线播放| 精品视频1区2区3区| 日韩中文字幕在线视频观看 | 欧美日韩成人一区| 婷婷精品进入| 欧美日韩中文精品| 在线视频不卡国产V| 久99久视频| 国产三级做爰在线观看| 国产精品一区二三区| 一区二区三区精品久久久| 国产丝袜一区| 国产成人精品日本亚洲专区61| 日韩精品大片| 国产日韩第一页v| 精品国产91乱高清在线观看| 视频一区三区| 在线中文免费视频| 中文av字幕一区| 在线视频色在线| 国产三级在线观看视频| 欧美一级在线免费| 韩日中文字幕第一页| 亚洲一级影院| 国产免费久久| 精品在线一区二区三区| 亚洲经典中文字幕| 一区二区国产在线| 久久精品网站免费观看| 日韩欧美三级在线| 亚洲一区精品在线| 成人一区二区不卡免费| 日韩手机在线观看视频| 午夜伊人狠狠久久| 91久久精品网| 中文字幕第一页在线| 日韩a一区二区| 综合图区亚洲白拍在线 | 日韩精品免费视频一区二区三区| 久久精品国产91精品亚洲| 中文字幕成人乱码在线电影| 日韩精品在线中文字幕| 日韩亚洲欧美中文字幕| 精品日韩视频| 日韩欧美亚洲视频| 日韩在线不卡| 国产成人精品亚洲| 一本一道久久a久久精品综合蜜臀| 99精品999| 中文字幕精品www乱入免费视频| 日韩欧美不卡在线| 亚洲一区视频在线观看视频| 久久精品蜜桃| 国产乱国产乱300精品| 日韩三级免费观看| 午夜高潮免费视频| 日韩久久99| 日韩欧美在线看| 欧美三级在线看| 日韩一级免费在线观看| 国产视频一区三区| 日本亚洲欧美三级| 国产黄色在线免费观看| 精品在线91| 欧美日韩国产中字| 亚洲免费看av| 中文字幕一区不卡| 不卡av免费在线| 精品在线一区二区| 蜜桃视频中文字幕| 精品视频国产| 在线中文字幕视频观看| 中文字幕久久精品| 日韩欧美不卡在线| 国产黄在线播放| 91欧美日韩在线| 亚洲免费精品| 日韩视频 中文字幕| 欧美日韩精品国产| 亚洲欧洲日韩精品在线| 精品成人久久av| 日韩av二区| 欧美日韩久久不卡| 999精品视频在线观看播放| 日本xxxwww免费视频| 久久久久蜜桃| 精品久久久精品| 国产免费不卡av| 97天天综合网| 国产一二三精品| 欧美三级日韩在线| 亚洲免费福利视频| 欧美日韩精品在线播放| 91精品国产综合久久香蕉最新版| 亚洲视频资源在线| 亚洲国产www| 在线视频第一页|