
在使用javascript的fetch api向php后端发送post请求时,开发者常会遇到php的$_post数组为空的情况。这通常是由以下两个主要原因造成的:
当Content-Type不是application/x-www-form-urlencoded时,PHP默认不会填充$_POST全局变量。例如,如果Content-Type被设置为application/text,PHP会将其视为原始文本,需要通过php://input流手动读取。
为了确保fetch POST请求能够正确地将参数传递给PHP,我们需要从请求头和请求体两方面进行修正和优化。
确保fetch选项对象中的headers键只出现一次,并且Content-Type被正确设置为application/x-www-form-urlencoded。
错误示例:
立即学习“PHP免费学习笔记(深入)”;
let respuesta = fetch(fichero, {
method: "POST",
headers: { // 第一次出现 headers
'Content-Type': 'application/x-www-form-urlencoded',
},
body: '...',
headers: {"Content-type": "application/text; charset=UTF-8"} // 第二次出现 headers,会覆盖第一次
})在上述代码中,headers键出现了两次,JavaScript会采用后面的值,导致实际发送的Content-Type是application/text; charset=UTF-8,而不是application/x-www-form-urlencoded。
正确修正后的请求头:
let respuesta = fetch(fichero, {
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded', // 确保只设置一次且正确
},
body: '...', // 请求体部分将在下文详细说明
})请求体需要将表单数据以key=value&key2=value2的形式组织起来,并且每个值都必须进行URL编码。以下是几种推荐的方法:
这种方法适用于参数较少或需要精细控制参数名称和值的情况。你需要手动将每个变量的值进行encodeURIComponent编码,然后通过模板字符串拼接成符合application/x-www-form-urlencoded格式的字符串。
示例代码:
const fichero = "/proves/php/accion_formulario.php";
let tp_curso = document.getElementById("actualizar_nombre").value;
let vr_curso = document.getElementById("version_lenguaje").value;
let pr_curso = document.getElementById("programa_curso").value;
let fp_curso = document.getElementById("ficheros_curso").value;
let vp_curso = document.getElementById("videos_curso").value;
let n_curso_actualizar = "curso_actualizar_value"; // 假设这是另一个值
let respuesta = fetch(fichero, {
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `nom=${encodeURIComponent(tp_curso)}&versio=${encodeURIComponent(vr_curso)}&programa=${encodeURIComponent(pr_curso)}&fitxers=${encodeURIComponent(fp_curso)}&videos=${encodeURIComponent(vp_curso)}&ncurs=${encodeURIComponent(n_curso_actualizar)}`,
})
.then(response => response.text())
.then(data => {
alert(data);
})
.catch(error => alert("Se ha producido un error: " + error));注意事项:
URLSearchParams接口提供了一种处理URL查询字符串的便捷方式。它可以接收一个对象作为构造函数的参数,自动构建并URL编码键值对。这是处理application/x-www-form-urlencoded类型请求体的推荐方法之一。
示例代码:
const fichero = "/proves/php/accion_formulario.php";
let tp_curso = document.getElementById("actualizar_nombre").value;
let vr_curso = document.getElementById("version_lenguaje").value;
let pr_curso = document.getElementById("programa_curso").value;
let fp_curso = document.getElementById("ficheros_curso").value;
let vp_curso = document.getElementById("videos_curso").value;
let n_curso_actualizar = "curso_actualizar_value";
const params = new URLSearchParams({
nom: tp_curso,
versio: vr_curso,
programa: pr_curso,
fitxers: fp_curso,
videos: vp_curso,
ncurs: n_curso_actualizar
});
let respuesta = fetch(fichero, {
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: params.toString(), // URLSearchParams对象会自动转换为适合body的字符串
})
.then(response => response.text())
.then(data => {
alert(data);
})
.catch(error => alert("Se ha producido un error: " + error));注意事项:
FormData接口提供了一种构建一组键/值对的方式,这些键/值对以与multipart/form-data请求相同的方式进行编码。虽然通常用于文件上传,但它也非常适合发送普通的表单数据,特别是当你的数据来源于一个HTML
以上就是解决Fetch POST请求参数无法正确传递到PHP的问题的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号