You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XMLHttpRequest无法向PHP发请求及log.js函数改造需求

Hey there! Let's tackle your two issues one by one—first converting that GET request to POST (to avoid relying on cookies for login info), then troubleshooting why your XMLHttpRequest isn't reaching the PHP file properly.

1. Convert refreshData to a POST Request

Here's the adjusted function with POST support, plus fixes for common issues:

function refreshData(file, msg) { 
    var xmlhttp = new XMLHttpRequest(); 
    // Switch to POST method, keep async flag explicit (true is default)
    xmlhttp.open("POST", file, true); 
    // Maintain the correct Content-Type for form-encoded data
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    
    // Encode your data as POST parameters (replace 'loginInfo' with your actual parameter name)
    var postData = new URLSearchParams();
    postData.append('loginInfo', msg); // Pass your login/request data here instead of via cookies
    
    xmlhttp.send(postData); 
    
    xmlhttp.onreadystatechange = function() { 
        // Fix the status check syntax (you had a typo with `=` instead of `===`)
        if (this.readyState === 4) {
            if (this.status === 200) {
                // Handle successful response (add your logic here)
                console.log('Success! Response:', this.responseText);
            } else {
                // Log errors to debug request failures
                console.error('Request failed. Status code:', this.status);
            }
        }
    }
}

Key changes explained:

  • Swapped GET for POST in xmlhttp.open()
  • Used URLSearchParams to safely encode POST data (avoids manual encoding bugs)
  • Passed the encoded data directly to xmlhttp.send() (POST data lives in the request body, not the URL)
  • Fixed the status check typo (this.status =... → this.status === 200)
  • Added error handling for non-200 status codes to help debug

On the PHP side, make sure you retrieve the data with $_POST['loginInfo'] (instead of $_GET or cookie-based checks) to match the POST request.

2. Troubleshooting XMLHttpRequest Request Failures

If your request still isn't reaching the PHP file, check these common issues:

  • Incorrect file path: Verify the file parameter points to the right location (use absolute paths like /api/your-file.php if relative paths are causing issues). Print the file value to the console to confirm.
  • Cross-Origin Resource Sharing (CORS) error: If your frontend runs on a different domain/port than your PHP backend, browsers block the request. Add CORS headers to your PHP file to allow access:
    header("Access-Control-Allow-Origin: https://your-frontend-domain.com"); // Replace with your actual frontend domain
    header("Access-Control-Allow-Methods: POST");
    
  • Server/PHP errors: Check your server's error logs (Apache/Nginx logs) or enable error reporting in PHP to catch issues like missing files, permission errors, or syntax mistakes:
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
  • DevTools Network tab: Open your browser's DevTools (F12), go to the Network tab, and look for the request. Check its status code, response body, and any error messages—this is the most direct way to diagnose what's going wrong.

内容的提问来源于stack exchange,提问作者misha sachenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:17:18