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

IIS环境下AJAX提交PHP数据格式错误问题及双方案需求

解决AJAX提交数据到PHP时的格式错误问题

看起来你在使用AJAX发送表单数据到PHP脚本时遇到了格式解析问题——数据出现了多余的边界符内容,而且section参数也没被正确接收。核心原因是你错误地给FormData请求手动设置了application/x-www-form-urlencoded的请求头,而FormData默认应该使用multipart/form-data格式,浏览器会自动处理对应的请求头(包括边界符),手动覆盖会导致服务器无法正确解析数据。

下面提供原生JavaScript和jQuery两种修复方案:

原生JavaScript 修复方案

方案1:使用FormData(推荐,支持文件上传)

只需要移除手动设置的Content-Type请求头,让浏览器自动处理即可:

<!DOCTYPE html>
<html style="height:100%;">
<head></head>
<body>
<script>
function zPostToTopic_ajax(){
    const url = 'http://the_server/infospace/php/infospace2.php';
    const formData2 = new FormData();
    formData2.append('section', 'general');
    formData2.append('action2', 'preview');
    
    const http_request = new XMLHttpRequest();
    http_request.open("POST", url);
    
    http_request.onreadystatechange = function() {
        if(http_request.readyState === 4 && http_request.status === 200){
            alert(http_request.responseText);
        }
    };
    
    // 移除这行错误的请求头设置,浏览器会自动添加正确的multipart/form-data头
    // http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
    http_request.send(formData2);
}
</script>
<form name="form_post_to_topic" id="form_post_to_topic" method="POST" action="http://the_server/infospace/php/infospace2.php">
&nbsp
<input type="button" value="Send" onclick="zPostToTopic_ajax();return false;">
</form>
</body>
</html>

方案2:发送application/x-www-form-urlencoded格式数据

如果你不需要上传文件,只想发送普通键值对,可以不用FormData,改用URLSearchParams拼接数据:

function zPostToTopic_ajax(){
    const url = 'http://the_server/infospace/php/infospace2.php';
    const data = new URLSearchParams();
    data.append('section', 'general');
    data.append('action2', 'preview');
    
    const http_request = new XMLHttpRequest();
    http_request.open("POST", url);
    
    http_request.onreadystatechange = function() {
        if(http_request.readyState === 4 && http_request.status === 200){
            alert(http_request.responseText);
        }
    };
    
    // 这里设置Content-Type是正确的,因为我们发送的是urlencoded格式
    http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http_request.send(data.toString());
}

jQuery 解决方案

用jQuery处理AJAX会更简洁,它会自动帮你处理请求头和数据格式:

方案1:发送FormData(支持文件上传)

<!DOCTYPE html>
<html style="height:100%;">
<head>
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>
<script>
function zPostToTopic_ajax(){
    const url = 'http://the_server/infospace/php/infospace2.php';
    const formData2 = new FormData();
    formData2.append('section', 'general');
    formData2.append('action2', 'preview');
    
    $.ajax({
        url: url,
        type: 'POST',
        data: formData2,
        processData: false, // 禁止jQuery自动处理FormData(避免破坏格式)
        contentType: false, // 让浏览器自动设置正确的Content-Type
        success: function(response){
            alert(response);
        }
    });
}
</script>
<form name="form_post_to_topic" id="form_post_to_topic" method="POST" action="http://the_server/infospace/php/infospace2.php">
&nbsp
<input type="button" value="Send" onclick="zPostToTopic_ajax();return false;">
</form>
</body>
</html>

方案2:发送urlencoded格式数据

如果只是普通键值对,直接传对象即可,jQuery默认会自动转成application/x-www-form-urlencoded格式:

function zPostToTopic_ajax(){
    const url = 'http://the_server/infospace/php/infospace2.php';
    
    $.ajax({
        url: url,
        type: 'POST',
        data: {
            section: 'general',
            action2: 'preview'
        },
        // jQuery默认Content-Type就是application/x-www-form-urlencoded,无需手动设置
        success: function(response){
            alert(response);
        }
    });
}

验证结果

修复后,你的PHP脚本print_r($_REQUEST);应该会输出正确的参数数组:

Array (
[section] => general
[action2] => preview
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 18:37:47