• 0

Convert javascript to use POST instead of GET


Question

I found a really cool script (link) except when I try to use it, it uses GET instead of POST. I don't want to use GET for what i'm using it for and would much rather use POST.

Here is the part of the script (I think) that makes it use POST.

<!--
//script by [url="http://www.yvoschaap.com//XMLHttpRequest"]http://www.yvoschaap.com

//XMLHttpRequest[/url] class function
function datosServidor() {
};
datosServidor.prototype.iniciar = function() {
 try {
  // Mozilla / Safari
  this._xh = new XMLHttpRequest();
 } catch (e) {
  // Explorer
  var _ieModelos = new Array(
  'MSXML2.XMLHTTP.5.0',
  'MSXML2.XMLHTTP.4.0',
  'MSXML2.XMLHTTP.3.0',
  'MSXML2.XMLHTTP',
  'Microsoft.XMLHTTP'
  );
  var success = false;
  for (var i=0;i < _ieModelos.length && !success; i++) {
   try {
    this._xh = new ActiveXObject(_ieModelos[i]);
    success = true;
   } catch (e) {
   }
  }
  if ( !success ) {
   return false;
  }
  return true;
 }
}

datosServidor.prototype.ocupado = function() {
 estadoActual = this._xh.readyState;
 return (estadoActual && (estadoActual < 4));
}

datosServidor.prototype.procesa = function() {
 if (this._xh.readyState == 4 && this._xh.status == 200) {
  this.procesado = true;
 }
}

datosServidor.prototype.enviar = function(urlget,datos) {
 if (!this._xh) {
  this.iniciar();
 }
 if (!this.ocupado()) {
  this._xh.open("GET",urlget,falsethis.this._xh.send(datos);
  if (this._xh.readyState == 4 && this._xh.status == 200) {
   return this._xh.responseText;
  }

 }
 return false;
}


var urlBase = "/microposts/";
var formVars = "";
var changing = false;


function fieldEnter(campo,evt,idfld) {
 evt = (evt) ? evt : window.event;
 if (evt.keyCode == 13 && campo.value!="") {
  elem = document.getElementById( idfld );
  remotos = new datosServidor;
  nt = remotos.enviar(urlBase + "" +encodeURIComponent(elem.id)+ "/"+encodeURIComponent(campo.value)+""+formVarsremove glow
  noLight(elem);
  elem.innerHTML = nt;
  changing = false;
  return false;
 } else {
  return true;
 }


}

function fieldBlur(campo,idfld) {
 if (campo.value!="") {
  elem = document.getElementById( idfld );
  remotos = new datosServidor;
  nt = remotos.enviar(urlBase + "" +encodeURIComponent(elem.id)+ "/"+encodeURIComponent(campo.value)+""+formVarsem.innerHTML = nt;
  changing = false;
  return false;
 }
}

//edit field created
function editBox(actual) {
 //alert(actual.nodeName+' '+changing);
 if(!changing){
  width = widthEl(actual.id) + 20;
  height =heightEl(actual.id) + 2;

  if(height < 40){
   if(width < 100) width = 150;
   actual.innerHTML = "<input id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" maxlength=\"254\" type=\"text\" value=\"" + actual.innerHTML + "\" onkeypress=\"return fieldEnter(this,event,'" + actual.id + "')\" onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\" />";
  }else{
   if(width < 70) width = 90;
   if(height < 50) height = 50;
   actual.innerHTML = "<textarea name=\"textarea\" id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\">" + actual.innerHTML + "</textarea>";
  }
  changing = true;
 }

  actual.firstChild.focus();
}



//find all span tags with class editText and id as fieldname parsed to update script. add onclick function
function editbox_init(){
 if (!document.getElementsByTagName){ return; }
 var spans = document.getElementsByTagName("span");

 // loop through all span tags
 for (var i=0; i<spans.length; i++){
  var spn = spans[i];

    	 if (((' '+spn.className+' ').indexOf("editText") != -1) && (spn.id)) {
   spn.onclick = function () { editBox(this); }
   spn.style.cursor = "pointer";
   spn.title = "Click to edit this post."; 
   	  }

 }


}

//crossbrowser load function
function addEvent(elm, evType, fn, useCapture)
{
 if (elm.addEventListener){
  elm.addEventListener(evType, fn, useCapture);
  return true;
 } else if (elm.attachEvent){
  var r = elm.attachEvent("on"+evType, fn);
  return r;
 } else {
  //alert("Please upgrade your browser to use full functionality on this page");
 }
}

//get width of text element
function widthEl(span){
 if(document.layers){
  w=document.layers[span].clip.width;
 } else if (document.all && !document.getElementById){
  w=document.all[span].offsetWidth;
 } else if(document.getElementById){
  w=document.getElementById(span).offsetWidth;
 }
 return w;
}

//get height of text element
function heightEl(span){

 if(document.layers){
  h=document.layers[span].clip.height;
 } else if (document.all && !document.getElementById){
  h=document.all[span].offsetHeight;
 } else if(document.getElementById){
  h=document.getElementById(span).offsetHeight;
 }
 return h;
}

function highLight(span){
 //span.parentNode.style.border = "2px solid #D1FDCD";
 //span.parentNode.style.padding = "0";
 span.style.border = "1px solid #54CE43";      	
}

function noLight(span){
 //span.parentNode.style.border = "0px";
 //span.parentNode.style.padding = "2px";
 span.style.border = "0px";   
}

//sets post/get vars for update
function setVarsForm(vars){
 formVars  = vars;
}

addEvent(window, "load", editbox_init);
-->

Thanks for your help :D

4 answers to this question

Recommended Posts

  • 0

So it does an ajax request using the Get method.

You can change that to do an ajax via a Post if you wish. Ajax doesn't care, but the response page does / might.

What is it that you actually want to do? Just change the method? That's one line of script.

document.forms[0].method = "post";

  • 0
datosServidor.prototype.enviar = function(urlget,datos) {
 if (!this._xh) {
 this.iniciar();
 }
 if (!this.ocupado()) {
 this._xh.open("GET",urlget,false= THIS, i think if you change to "post" it will always do a post, the code does not seem to catter to post/get it's hardcoded for get only
 this._xh.send(datos);
 if (this._xh.readyState == 4 && this._xh.status == 200) {
 return this._xh.responseText;
 }

 }
 return false;
}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Ironically, I do have audio engineering experience, but still find the different flavors of VB-Audio's VoiceMeeter (and more advanced options like Banana and Potato) to be a little unintuitive. Yes, I can figure them out, but despite having a UI that looks like an audio mixer, they don't exactly follow typical audio mixing conventions, so it takes a little time to figure out how the software works. Still, I feel like for what you are asking for, VoiceMeeter is a good solution. If you're interested, I could probably create a quick how-to video.
    • If it was not for AI push, you have a point but as I already use notepad++ and I really don’t see the need for all the extras in a damn NOTEPAD… I also disagree real notepad with pen does not offer spell check, so why NOTEPAD app should? you already have free Word online.. if this is what you need.
    • How is it "one of the best file managers in Windows"? It is slow as hell and crashes like it's the offspring of Windows Me
    • Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Blocked] "{e2bf9676-5f8f-435c-97eb-11607a5bedf7}"="Share" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Blocked] "{e2bf9676-5f8f-435c-97eb-11607a5bedf7}"="Share" [-HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers\ModernSharing] Remove that Share text.
    • Meh could never use the UWP as I use WhatsApp business and it's features are available on the standard. Thos this applies to both windows and macos+ipad versions. So I'm stuck on using web versions for whatever platform.
  • Recent Achievements

    • Rookie
      Snake Doc went up a rank
      Rookie
    • First Post
      nobody9 earned a badge
      First Post
    • One Month Later
      Ricky Chan earned a badge
      One Month Later
    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      497
    2. 2
      Michael Scrip
      205
    3. 3
      ATLien_0
      201
    4. 4
      Xenon
      138
    5. 5
      +FloatingFatMan
      117
  • Tell a friend

    Love Neowin? Tell a friend!