2014-12-13 73 views
1

當我加載我的index.html站點時,它有時會返回Ajax,它會調用服務器上的php來打印我的動態頁面的結果。爲什麼只有當我進入地址欄並按回車或刷新時纔有效?Ajax調用服務器PHP只有時打印回請求

這是網站Click Here!

這一直困擾着我了一段時間了。

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title>Christmas- 2014: Secret Santa</title> 
 
    <meta charset="UTF-8"> 
 
    <link rel="stylesheet" type="text/css" href="css/styleSheet.css"> 
 
    <link rel="icon" type="icon" href="images/favicon.ico"> 
 

 
    <script> 
 
    var xmlhttp; 
 
    if (window.XMLHttpRequest) { 
 
     //code for IE7, Firefox and everything new.... 
 
     xmlhttp = new XMLHttpRequest(); 
 
    } else { 
 
     //for the old things 
 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
 
    } 
 

 

 

 

 
    xmlhttp.onreadystatechange = function() { 
 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
 
     document.getElementById("mainDynamic").innerHTML = xmlhttp.responseText; 
 
     } 
 

 

 

 

 

 
     $(document).ready(function() { 
 
     $("#export_excel_button").click(function(e) { 
 

 
      window.open('data:application/vnd.ms-excel,' + $('#userTable').html()); 
 
      e.preventDefault(); 
 
     }); 
 

 
     }); 
 

 

 
    } 
 

 
    xmlhttp.open("GET", "resourceLines.php", true); 
 
    xmlhttp.send(); 
 
    </script> 
 
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script> 
 
    <script type="text/javascript"> 
 
    </script> 
 
</head> 
 

 
<body id='mainBody'> 
 
    <img class="Banner" src="images/Banner.JPG" alt="Banner" width="886" height="187"> 
 
    <section> 
 
    <div id='mainDynamic'> 
 

 

 
    </div> 
 
    <article class='leftSide'> 
 
     <div class="Presentations"> 
 
     <h1>Important Information</h1> 
 
     <b>WebSite Scheme:</b> 
 
     <br> 
 
     <p>The Purpose of this is to have computer generated randomization of the "Secret Santa gifting". All wish list can be made online and will be automatically uploaded to the server. Then, I will run the program to randomly pair you then display the 
 
      wish list to whom you'll be a secret Santa to.</p> 
 

 
     <p>So things to do: 
 
      <p> 
 
      <ul> 
 
       <li>Make an account</li> 
 
       <li>Build your wish list</li> 
 
      </ul> 
 
      <p><b>WARNING</b>: It is important that you make only ONE! account for yourself. 
 
       <p> 
 

 
       <p> 
 
        <b>Gifting Criteria:</b> 
 
        <br> 
 

 
        <ul> 
 
        <li><b>Amount of Gifts-</b> Each person should have at least 3 gifts for the gift-er to chose from</li> 
 
        <li><b>Price of Total Gifts-</b> $50</li> 
 
        <li>Total Amount of gifts you buy for the person must be close to equal or more than $50</li> 
 
        </ul> 
 
        <br> 
 
        <b>Commonly Asked Questions</b> 
 
        <ul> 
 
        <li>Can we list multiple gifts on our wish list to add up to the <b>MAX</b> dollar amount? Yes, if there are multiple low priced items, you will have to chose to buy enough gifts to add up to the total amount.</li> 
 
        </ul> 
 
       </p> 
 
     </div> 
 
    </article> 
 
    </section> 
 
</body> 
 

 
</html>

+0

你不允許使用JQuery的AJAX方法? – 2014-12-13 05:05:32

+0

可能與尚未加載的文檔有關。你有任何錯誤?您是否嘗試將JavaScript代碼移動到頁面底部,即在結束''標記之前? – 2014-12-13 05:06:02

+1

在實際使用之前,還應該包含jQuery庫。查看控制檯中的錯誤。 – 2014-12-13 05:08:13

回答

1

我檢查您的網站,建議低於:

  1. 的錯誤是,當你使用$(document).ready(function() {...}(這是你的XMLHttpRequest內),沒有加載jQuery的。所以,它導致錯誤Uncaught ReferenceError: $ is not defined

  2. 首先加載jQuery,你的錯誤不會再發生。

  3. 如果您使用jQuery,爲什麼不使用$.ajax? jQuery爲你瀏覽器兼容!下面

代碼:

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script> 
<script> 
$(function() { // execute after dom loaded 
    $.ajax({ 
     url: 'resourceLines.php' 
    }).done(function(res) { 
     // do whatever you like 
    }); 
}); 
</script>