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

如何修复因URL携带数据导致的414 Request URI too long错误?

解决414 Request URI too long错误的方案

你遇到的问题根源很明确:虽然用了POST请求,但还是把content数据拼接到URL参数里,当内容过长时就会触发414错误。POST请求的正确做法是把数据放在请求体中,而非URL里。

修改后的前端代码

function makePage(content1){   
    var content = content1;
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState === 4 && xmlhttp.status === 200){
            alert("webpage " + xmlhttp.responseText + " was successfully created!");
        } else {
            console.log("hata");
        }
    };
    // 打开请求时只指定接口地址,不再拼接参数
    xmlhttp.open("POST", "makePage.php", true);
    // 设置请求头,告诉服务器请求体的数据格式
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    // 把数据作为send方法的参数传入,放在请求体中,同时编码避免特殊字符干扰
    xmlhttp.send("content=" + encodeURIComponent(content));
}

后端PHP代码调整

你的makePage.php里如果原来用$_GET['content']获取数据,现在要改成$_POST['content']来接收请求体中的数据:

<?php
$content = $_POST['content'];
// 后续页面创建逻辑...
?>

关键说明

  • encodeURIComponent()会对内容中的特殊字符(如&、空格、中文等)进行编码,避免破坏请求体的格式
  • POST请求的请求体没有URL那样严格的长度限制(具体上限取决于服务器配置,但远大于URL的常规限制),能完美适配长内容的传输场景

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 01:55:33