3

イベントハンドラーとしてjavascriptを使用してpost/getメソッドのいずれかでhttpリクエストを送信するにはどうすればよいですか?ありがとう!ポール

4

6 に答える 6

4

さて、あなたは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();
  }

https://developer.mozilla.org/en/DOM/formを参照してください

于 2009-10-25T09:18:18.717 に答える
1

JavaScriptからリクエストを送信するためにXMLHttpRequestを使用できます

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また、ユーザー入力の場合は、パラメータ値のエンコードに使用してパラメータをエンコードすることを忘れないでください

例えば

params="paramName="+encodeURIComponent(paramValue);
于 2009-10-25T08:01:19.870 に答える
1

XmlHttpRequestを使用する

サンプルコード:

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

function handler()
{
   // your handler
}
于 2009-10-25T08:01:49.820 に答える
1

これを行うための標準クラスはですがXmlHttpRequest、普遍的にサポートされているわけではありません。一部のブラウザでは、ActiveXObject("Microsoft.XMLHTTP")代わりに使用する必要があります。

基盤となるブラウザーAPIに関係なくHTTPダウンロード(AJAXスタイル)メソッドを提供するjQueryシステムを調べます(したがって、Tzuryの回答に示されているコードの多くを避けます)。

jQueryAJAXのドキュメントはhttp://docs.jquery.com/Ajaxにあります

于 2009-10-25T08:13:09.737 に答える
1

非表示フィールドにatringを追加してから、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>
于 2010-06-28T07:56:40.463 に答える
0

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");
        }
    }
}
于 2009-10-25T08:04:26.930 に答える