Domanda

Come inviare una richiesta http con il metodo post / get usando javascript come gestore di eventi? Grazie! Paul

È stato utile?

Soluzione

Okay, non vuoi usare Ajax. Puoi usare un gestore di eventi per inviare un modulo!

<a href='#' onclick='cow_submit("zoodle")'>send</a>
<form method='post' id='formie' action='find_some_action.php'>
  <input type='hidden' id='snoutvar' name='snoutvar' value='snout'>
</form>

<script>
function cow_submit(a_var_to_set){
  var plip=document.getElementById('formie');
  var snout=document.getElementById('snoutvar');
  snout.value=a_var_to_set;
  plip.submit();
  }

Vedi https://developer.mozilla.org/en/DOM/form

Altri suggerimenti

usa XmlHttpRequest

codice di esempio:

var client = new XMLHttpRequest();
client.onreadystatechange = handler;
client.open("GET", "test.xml");
client.send();

function handler()
{
   // your handler
}

Puoi utilizzare XMLHttpRequest per inviare richieste da javascript

Invio richiesta GET

var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("GET", url+"?"+params, true);
http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(null);

Invio richiesta POST

var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(params);

E non dimenticare di codificare i parametri utilizzando encodeURIComponent per la codifica del valore param in caso di input dell'utente

per es.

params="paramName="+encodeURIComponent(paramValue);

La classe standard per farlo è XmlHttpRequest , ma non è universalmente supportata. Su alcuni browser devi invece utilizzare ActiveXObject (" Microsoft.XMLHTTP ") .

Guarda nel jQuery che fornisce metodi di download HTTP (stile AJAX) indipendentemente dalle API del browser sottostante (quindi evitando gran parte del codice mostrato nella risposta di Tzury).

La documentazione di jQuery AJAX è disponibile all'indirizzo http://docs.jquery.com/Ajax

Dovresti provare ad aggiungere una stringa in un campo nascosto e quindi chiamare form.submit () per inviare il modulo nella pagina definita in azione.

<script type="text/javascript">
  function doTestFormSubmit(yourString) {
    document.getElementById("myString").value=myString;
    document.getElementById("testForm").submit();
  }
</script>
<form name="testForm" id="testForm" action="yourDesiredPage.php" method="post">
   <input type="hidden" name="myString" id="myString" value=""/>
</form>

Ajax Tutorial ( http://code.google.com/ edu / ajax / tutorial / ajax-tutorial.html )

var obj;

function ProcessXML(url) {
  // native  object

  if (window.XMLHttpRequest) {
    // obtain new object
    obj = new XMLHttpRequest();
    // set the callback function
    obj.onreadystatechange = processChange;
    // we will do a GET with the url; "true" for asynch
    obj.open("GET", url, true);
    // null for GET with native object
    obj.send(null);
  // IE/Windows ActiveX object
  } else if (window.ActiveXObject) {
    obj = new ActiveXObject("Microsoft.XMLHTTP");
    if (obj) {
      obj.onreadystatechange = processChange;
      obj.open("GET", url, true);
      // don't send null for ActiveX
      obj.send();
    }
  } else {
    alert("Your browser does not support AJAX");
  }
}


function processChange() {
    // 4 means the response has been returned and ready to be processed
    if (obj.readyState == 4) {
        // 200 means "OK"
        if (obj.status == 200) {
            // process whatever has been sent back here:
        // anything else means a problem
        } else {
            alert("There was a problem in the returned data:\n");
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top