eine HTTP-Anforderung ohne XHR in einem Event-Handler senden
-
06-07-2019 - |
Frage
Wie mit einer HTTP-Anfrage senden entweder post / get-Methode mit Hilfe von Javascript als Eventhandler? Vielen Dank! Paul
Lösung
Okay, Sie wollen nicht, Ajax verwenden. Sie können einen Ereignishandler verwenden, um ein Formular abzusenden!
<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();
}
Andere Tipps
Beispielcode:
var client = new XMLHttpRequest();
client.onreadystatechange = handler;
client.open("GET", "test.xml");
client.send();
function handler()
{
// your handler
}
Sie können mit XMLHttpRequest Anfrage von Javascript Senden
Senden von GET-Anfrage
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);
Senden von POST-Anfrage
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);
Und vergessen Sie nicht, Parameter zu kodieren für param Wertcodierung bei Benutzereingaben mit encodeURIComponent
z.
params="paramName="+encodeURIComponent(paramValue);
Die Standardklasse dafür ist XmlHttpRequest
, aber es ist nicht allgemein unterstützt. Bei einigen Browsern haben Sie ActiveXObject("Microsoft.XMLHTTP")
stattdessen verwenden.
Schauen Sie in die jQuery System, die HTTP-Download bietet (AJAX-Stil) Methoden unabhängig von dem zugrunde liegenden Browser-APIs (daher eine Menge des Codes zu vermeiden in Tzury Antwort gezeigt).
Die jQuery AJAX-Dokumentation ist unter http://docs.jquery.com/Ajax
Sie sollten versuchen Zeichenfolge in einem versteckten Feld hinzuzufügen und dann die form.submit () aufrufen, das Formular in die Seite einreichen definieren in Aktion.
<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 / tutorials / 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");
}
}
}