请求PHP文件获取日期时间无输出,Ajax新手寻求问题排查帮助
Hey there! As an Ajax beginner, it’s totally normal to hit this kind of snag—let’s break down the common issues and fix this step by step.
First, let’s go through the most likely culprits:
1. Your loadDoc() function isn’t being triggered!
Looking at your code, you’ve defined the Ajax function but never told it to run. Ajax functions don’t execute automatically unless you call them explicitly. Try one of these quick fixes:
- Run it when the page loads: Add this line right after your function definition in the script:
window.onload = loadDoc; - Add a clickable button: Drop this somewhere in your
<body>to let you manually trigger the request:<button onclick="loadDoc()">Get Server Time</button>
2. Verify your PHP file works on its own
Before digging into Ajax, make sure the PHP side is functioning correctly:
- Open your browser and navigate directly to
http://localhost/polling.php(replacelocalhostwith your server’s address if you’re using a remote host).- If you see the formatted date string, great—PHP is working as expected.
- If you see the raw PHP code instead, your web server (like Apache) isn’t set up to parse PHP files. Make sure you’re running a server stack like XAMPP/WAMP and that Apache is started.
- If you get a 404 error, the file path is wrong. Double-check that
polling.phpis in the same folder as your HTML file, or adjust the path inxhttp.open()(e.g.,"../polling.php"if it’s one directory up).
3. Use browser dev tools to debug the request
Browsers have built-in tools to show exactly what’s happening with your Ajax call:
- Press F12 to open Developer Tools.
- Switch to the Network tab.
- Trigger your
loadDoc()function (refresh the page or click the button you added). - Look for the
polling.phpentry in the request list:- Status 404: File not found—fix the file path in your JS.
- Status 500: PHP error—check your PHP code for typos (even extra spaces before
<?phpcan cause issues). - Status 200: Click the entry and check the Response tab. If it shows the date, the problem is likely in your JS updating the DOM (but your
getElementById("poll")selector looks correct).
4. Cleaned-up code to test with
Here’s a revised version of your code with small tweaks to avoid common pitfalls:
Updated HTML/JavaScript:
<body> <p>Current server time:</p> <button onclick="loadDoc()">Get Server Time</button> <div id="poll"></div> <script> function loadDoc() { var xhttp = new XMLHttpRequest(); // Use onload for simpler success handling xhttp.onload = function() { if (this.status === 200) { document.getElementById("poll").innerHTML = this.responseText; } else { console.log("Request failed with status: " + this.status); } }; // Handle network errors xhttp.onerror = function() { console.log("Network error occurred"); }; xhttp.open("GET", "polling.php", true); xhttp.send(); } </script> </body>
Updated PHP (cleaned up for consistency):
<?php echo date("D M j H:i:s e Y"); ?>
Start with these checks—most likely the issue is either the function not being called or a simple file path mistake. Let me know if you still run into problems after trying these!
内容的提问来源于stack exchange,提问作者SavageBooBoo

