イベントハンドラーでXHRを使用せずにhttpリクエストを送信する
-
06-07-2019 - |
質問
イベントハンドラーとしてjavascriptを使用してpost / getメソッドでHTTPリクエストを送信する方法は?ありがとう!ポール
解決
さて、Ajaxを使いたくありません。 イベントハンドラを使用してフォームを送信できます!
<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();
}
他のヒント
を使用します。
サンプルコード:
var client = new XMLHttpRequest();
client.onreadystatechange = handler;
client.open("GET", "test.xml");
client.send();
function handler()
{
// your handler
}
XMLHttpRequest を使用して、javascriptからリクエストを送信できます
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);
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);
また、ユーザー入力の場合のパラメーター値のエンコードに encodeURIComponent
を使用してパラメーターをエンコードすることを忘れないでください
e.g。
params="paramName="+encodeURIComponent(paramValue);
これを行うための標準クラスは XmlHttpRequest
ですが、普遍的にサポートされていません。一部のブラウザでは、代わりに ActiveXObject(&quot; Microsoft.XMLHTTP&quot;)
を使用する必要があります。
基礎となるブラウザーAPIに関係なくHTTPダウンロード(AJAXスタイル)メソッドを提供する jQuery システムを調べます(したがって、 Tzuryの答えに示されている多くのコードを避けます)。
jQuery AJAXドキュメントは http://docs.jquery.com/Ajax にあります。 p>
非表示フィールドに文字列を追加してから、form.submit()を呼び出して、アクションで定義するページにフォームを送信する必要があります。
<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チュートリアル( 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");
}
}
}
所属していません StackOverflow