Jesdisciple Posted October 20, 2007 Share Posted October 20, 2007 I'm trying to get AJAX to work between my browser and localhost with the pretense that I need to keep some logic (i.e., for a bank script) confidential in a single-page app. Somewhere between function getXmlHttpObject(){ var xmlHttp; try{ // Firefox, Opera 8.0+, Safari xmlHttp = new XMLHttpRequest(); }catch (e){ // Internet Explorer try{ xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }catch (e){ try{ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("Your browser does not support AJAX!"); xmlHttp = null; } } } return xmlHttp;}function ClosedScript(url){ var _this = this; var xmlHttp = getXmlHttpObject(); xmlHttp.onreadystatechange = function(ev){ switch(xmlHttp.readyState){ case 0: break; case 1: break; case 2: break; case 3: break; case 4: _this.respond(xmlHttp.responseText); break; } }; xmlHttp.open('POST', url, true); this.request = function(query, responseFunc){ if(typeof(responseFunc) == 'function' && responseFunc.length == 1){ xmlHttp.setRequestHeader("Content-Length", query.length); xmlHttp.send(query); this.respond = responseFunc; }else{ throw 'IllegalArgumentException: \'responseFunc\' in \'request(query, responseFunc)\' must be a function which takes exactly one argument.'; } };}ClosedScript.prototype = { constructor: ClosedScript, respond: null}; ClosedScript.php: <?php print_r($_REQUEST); if(isset($_POST['content'])){ switch($_POST['content']){ case 'event': if(isset($_POST['q'])){ switch($_POST['q']){ case 'load': echo "eval:alert('onload detected at localhost')"; break; } } break; } }?> Any observations? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 22, 2007 Share Posted October 22, 2007 If you're using post you need a content-type header:xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); Link to comment Share on other sites More sharing options...
Jesdisciple Posted October 23, 2007 Author Share Posted October 23, 2007 It works, now; thanks! No tutorial I've found yet addresses POSTs. (I'm not still depending on an inference only because of Wikipedia.) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.