nction. This is a simplified example in that we’re simply ignoring any tags except those that describe each headline. In a true example (i.e., one that was retrieving more complex XML), the parsing code would be accordingly more complex, but I leave that as an exercise to the reader. Let’s finish up by examining the script for the example that demonstrates submitting data with a request. Just the script in the <head> is: var req; var which; function submitData() { // Construct a CSV string from the entries. Make sure all fields are // filled in first. f = document.theForm.firstName.value; m = document.theForm.middleName.value; l = document.theForm.lastName.value; a = document.theForm.age.value; if (f == "" || m == "" || l == "" || a == "") { alert("Please fill in all fields first"); return false; } csv = f + "," + m + "," + l + "," + a; // Ok, so now we retrieve the response as in all the other examples, // except that now we append the CSV onto the URL as a query string, // being sure to escape it first. retrieveURL("example5Submit.do?csv=" + escape(csv)); } function retrieveURL(url) { if (window.XMLHttpRequest) { // Non-IE browsers req = new XMLHttpRequest(); req.onreadystatechange = processStateChange; try { req.open("GET", url, true); } catch (e) { alert(e); } req.send(null); } else if (window.ActiveXObject) { // IE req = new ActiveXObject("Microsoft.XMLHTTP"); if (req) { req.onreadystatechange = processStateChange; req.open("GET", url, true); req.send(); } } } function processStateChange() { if (req.readyState == 4) { // Complete if (req.status == 200) { // OK response document.getElementById("theResponse").innerHTML = req.responseText; } else { alert("Problem: " + req.statusText); } } } In this example we are constructing a simple comma-separated string based on the inputs of the user. You can of course construct an XML document and send that, and in fact that is the more common example. But that is part of the reason I DIDN’T do it: I wanted to show you that you do not have to send XML when using XMLHttpRequest. In fact, this example does nothing more than append the CSV string onto the URL. There are numerous examples on the web for constructing a true XML document and submitting that using the XMLHttpRequest.send() method, and I highly recommend looking those up if you intend to use that approach. I hope that this rather short article and accompanying webapp have given you a good starting point from which to explore what the XMLHttpRequest object provides. Before I close I want to also point out that the Ajax concept itself does NOT require you to use XMLHttpRequest. You can get very much the same basic effect in other ways, including code in a hidden frame taking the place of XMLHttpRequest. This is in fact the technique I used five years ago in the project I spoke of at the beginning. However, XMLHttpRequest does make the underlying Ajax concept far easier to implement, and more standard. Just look to Google as a great example of the power of this technique. However, I caution anyone from thinking this is the way all web apps should be developed. I do not for a second advocate this as the One True Solution (for all you LOTR fans out there!). It is a good approach in many cases, but will not be appropriate in others. If reaching the maximum possible audience is your goal, you would want to stay away from this. If a user disabling scripting in their browser might be a concern (and your site wouldn’t be any good without it), this probably isn’t a good answer either. There are other reasons to stay away from it in some situations, but the bottom line is treat it like any other tool in your toolbox: it will be right for some jobs, maybe not so for others. After all, you don’t drive a nail with a glue gun, right?? In any case, I hope this has given you some food for thought. Have fun! About the authorFrank W. Zammetti is a Web Architect Specialist for a leading worldwide financial institution during the day and a PocketPC developer at night. He is the Founder and Chief Software Architect of Omnytex Technologies. He has over 10 years of professional development experience, and nearly 15 more of “amateur” experience, and he has been developing web-based applications (Intranet applications mostly) almost exclusively for nearly 7 years. Frank holds numerous certifications including SCJP, MCSD, CNA, i-Net+, A+, CIW, MCP and numerous BrainBench certifications. He is a contributor to a number of open-source projects, including DataVision, as well as having started two, including the Struts Web Services Enablement Project (search strutsws on SourceForge, or http://sourceforge.net/projects/strutsws/). Frank’s resume is available online at http://www.zammetti.com/resume. Sample webapp: http://www.omnytex.com/articles/xhrstruts/xhrstruts.zip PDF copy of this article: http://www.omnytex.com/articles/xhrstruts/xhrstruts.pdf
上一页 [1] [2] [3] [4] [Web开发]AJAX来判断dedecms用户是否登录 [系统软件]Explanation of UFT-8 and Unicode [系统软件]Using dllimport and dllexport in C++ Classes [常用软件]神奇 我家的照片会唱歌 照片会唱歌 [VB.NET程序]VB.NET and C# 语法比较手册 [Delphi程序]IntraWeb And ActiveForms [Delphi程序]different between BPL and DLL [VB.NET程序]How to setup a basic Struts project using Ecli… [网页制作]Classes and Objects in PHP5 [网页制作]新手解读:认识XML,AJAX,SNS,Tag
|