资源描述:
《Ajax, Getting Started with.pdf》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、#2CONTENTSINCLUDE:nGettingtoKnowHTTPnTipsforUsingXHRnAjaxandArchitectureGettingStartedwithAjaxnAjaxToolkitsnAjaxUserInterfacesByDaveCranenHotTipsandmore...visitrefcardz.comGETTINGSTARTEDGETTINGTOKNOWHTTPThestandardwaytodoAjaxistousetheXMLHttpRequestobject
2、,TomakeuseoftheXHRtoitsfullest,werecommendyouknownasXHRbyitsfriends.UseXHRdirectly,orviaoneofthebecomefamiliarwiththeworkingsoftheHTTPprotocol.UsinghelpfulAjaxlibrariessuchasPrototypeorjQuery.HowdoweuseAjax,youhavemuchmorecontroloverHTTPthanwithclassicXHR
3、“byhand”?Tostartwith,weneedtogetareferencetoit:webappdevelopment.if(window.XMLHttpRequest){requestxhr=newXMLHttpRequest();}elseif(window.ActiveXObject){xhr=newActiveXObject(“Microsoft.XMLHTTP”);}headersWecanthenopenaconnectiontoaURL:browserbodyxhr.open(“G
4、ET”,“my-dynamic-content.jsp?id=”+encodeURI(myId),servertrue);Specifyacallbackfunctiontoreceivetheresponse:headersbodyxhr.onreadystatechange=function(){responseprocessReqChange(req);}HTTPisastatelessrequest-responseprotocol.andthensendtherequest:xhr.send(n
5、ull);nBothrequestandresponsecontainheadersandanoptionalTheservermaybebusy,orthenetworkmaybeslow.Wedon’twantbody,whichisfreetext.tositarounddoingnothinguntiltheresponsearrives,andbecausenOnlyaPOSTrequestcontainsabody.www.dzone.comGetMoreRefcardz!we’veassig
6、nedthecallbackfunction,wedon’thaveto.That’sthenArequestdefinesaverbormethod.five-minuteguidefortheimpatient.Forthosewholiketoknowthedetails,we’velistedthefullerdetailsoftheXHRobjectbelow.nTheMimetypeofrequestandresponsecanbesetbytheheaderContent-typeMetho
7、dNameParametersandDescriptionsopen(method,url,async)openaconnectiontoaURLmethod=HTTPverb(GET,POST,etc.)NotallMicrosoftbrowsersrelyonActiveX.url=urltoopen,mayincludequerystringHotasync=whethertomakeasynchronousrequestTipIE7providesanativeJavaScriptXHR,sowe
8、checkforthatfirst.onreadystatechangeassignafunctionobjectascallback(similartoonclick,onload,etc.inbrowsereventmodel)setRequestHeaderaddaheadertotheHTTPrequest(namevalue)Ajaxsend(body)sendtherequestGetMoreRefcardzbod