Just a continuation of the last post.
This XMLHttpRequest implementation straightens things out a bit. And, yes, I am well aware that there are better ways to do null checking. But if I did everything for you, you’d never have to learn anything for yourself…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | function isObject(obj) { var bIsObject = false ; if ( obj ) { bIsObject = ( typeof obj== 'object' ); } return bIsObject; } function getObjectProperty( obj, strPropName ) { var prop = null ; if ( isObject(obj) && strPropName ) { if ( obj.hasOwnProperty(strPropName) ) { prop = obj[strPropName]; } } return prop; } function doHttpRequest( strUrl, objParams ) { var xmlhttp = null ; try { // for all modern browsers xmlhttp = new XMLHttpRequest(); } catch (e1) { try { // for IE5 and IE6 xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" ); } catch (e2) { alert( "XMLHttpRequest object could not be created..." ); } } if ( !xmlhttp ) { alert( "XMLHttpRequest object could not be created..." ); } else { var objRequestData = getObjectProperty(objParams, 'objRequestData' ); var bDoPost = getObjectProperty(objParams, 'bDoPost' ); var funcCallback = getObjectProperty(objParams, 'funcCallback' ); var objCallbackParams = getObjectProperty(objParams, 'objCallbackParams' ); if ( !strUrl ) { alert( 'URL cannot be blank...' ); } else { var strRequestData = '' ; if ( objRequestData ) { for ( var strKey in objRequestData ) { var strValue = objRequestData[strKey]; if ( strRequestData.length>0 ) { strRequest += "&" ; } strRequestData += encodeURIComponent(strKey) + "=" + encodeURIComponent(strValue); } } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4) { if ( xmlhttp.status!=200 ) { alert( "Error in HTTP Request...\n\nStatus: " +xmlhttp.status+ "\n" +xmlhttp.statusText); } else { if (!objCallbackParams) { objCallbackParams = { 'xmlhttp' :xmlhttp }; } else { objCallbackParams.xmlhttp = xmlhttp; } if ( funcCallback ) { funcCallback(objCallbackParams); } else { alert( 'http request done...' ); } } } } if ( bDoPost ) { xmlhttp.open( "POST" , strUrl, true ); xmlhttp.setRequestHeader( "Content-type" , "application/x-www-form-urlencoded" ); xmlhttp.send(strRequestData); } else { xmlhttp.open( "GET" , strUrl+ "?" +strRequestData, true ); xmlhttp.send( null ); } } } } |