0

何が間違っているのかわかりませんが、すべてが良さそうです。私は取り組んでいlocalhostますが、ファイルを読み込もうとして問題が発生しています。

これは私のコードです。私は NetBeans で作業していますが、コンソールはエラーなしでクリアです。

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc() {
    var xmlhttp;
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST", "demo_post.php", true);
    xmlhttp.send();
}
</script>
</head>
<body>

<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>

</body>
</html>

このコードを実行すると、結果が得られません。

4

3 に答える 3

0

あなたのコードは実際にはGETメソッド用に設計されていることがわかりました.そして、この場合、getの代わりにPOSTを使用することは重要ではありません.(パラメータが渡されず、フォームではないため..)また、@Jacksonに同意します

  <!DOCTYPE html>
  <html>
  <head>
  <!--you can use online js file but in here i download the js file from       code.jquery.com/jquery-2.0.3.min.js and kept it in localhost same folder -->
  <script type="text/javascript" src="/jquery-2.0.3.min.js"></script>
  <script type="text/javascript">
  function loadXMLDoc()
  {
  var xmlhttp;
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
  else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function()
  {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
  document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  }
 }
 xmlhttp.open("GET","demo_post.php",true);
 xmlhttp.send();
 }
 </script>
 </head>
 <body>
 <h2>AJAX</h2>
 <button type="button" onclick="loadXMLDoc()">Request data</button>
 <div id="myDiv"></div>

 </body>
 </html>
于 2013-07-31T19:59:56.557 に答える
0

.open() と .send() の呼び出しの間に、リクエスト ヘッダーを次のように設定します。

xmlhttp.open("POST", "demo_post.php", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send();

少なくとも、jQuery を使用したくない場合は、このようにします。

于 2013-07-31T20:01:00.243 に答える
0

いくつかのコメントが示唆しているように、ブラウザのエラー コンソールを確認する必要があります。NetBeans ではありません。また、JS などでブレークポイントを設定する方法を理解する。

以下は、純粋な Javascript を使用するよりもはるかに単純な jQuery を使用して達成しようとしているものの例です。

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script type="text/javascript">

   // jQuery allows you to use selectors rather than onClick, etc
   // So when anything with a class called "loadData" is clicked this function will run       
   $(".loadData").click(function (event) {

       $.ajax({
          url: 'demo_post.php',  // The URL that your making the request to
          type: 'POST', // Type - GET or POST
          dataType: 'html', // DataType - can be html, json or jsonp
          cache: false, // true or false - whether you want data to be cached
          data: 'foo=bar', // Any data that your submitting with the request.
          error: function (error_response) {

             // An error has occured so empty your #myDiv and put the error in there.
             $("#myDiv").empty().append(error_response.status);

          },
          success: function (response) {

             // Everything has worked - empty #myDiv and put the replace with response
             $("#myDiv").empty().append(response);

          }
       });

   });

</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" class="loadData">Request data</button>
<div id="myDiv"></div>

</body>
</html>

jQuery の詳細については、http: //api.jquery.comを参照してください。具体的には、jQuery の AJAX 関数については、 http://api.jquery.com/jQuery.ajax/ を参照してください。

于 2013-07-31T19:34:19.593 に答える