javascript - 从 JQuery.post() 中恢复数据
问题描述
我正在使用 a$.post()
将数据推送到php
文件中。但是$_POST
当我制作它时它是空var_dump
的。
JS script
:
$('#Notes').on("click", function (e) {
e.preventDefault();
let $id = $(document).getUrlParam("varname");
let $text = $(this).attr('data');
let $notes = prompt("Modifier la note", $text);
console.log($text, $id, $notes);
if ($notes !== null || $notes !== "") {
$.post(
'../buckets/update_note.buckets.php',
{
id: $id,
notes: $notes,
},
function (data) {
console.log('Data Id : ',data.id);
console.log('Data Name : ',data.name);
})
.then(r =>{
location.replace('../buckets/update_note.buckets.php');
})
}
在php
文件中:
<?php
var_dump($_POST);
var_dump($_GET);
第一个console.log
向js
我展示了 3 个变量的值,但console.log
在回调中向我展示了undefined
。但我在网络调试器中看到:
Form Data
id: xxxx
notes: xxxx
任何想法 ?
解决方案
我相信,在您的情况下,数据将是文本。你需要json_encode()
在你的 php 中使用来打印复杂的对象。然后在您的 JS 中,您可以使用它JSON.parse()
来恢复对象或在查询中定义application/json
内容类型。POST
就像是:
// php
$json = json_encode([
'post' => $_POST,
'get' => $_GET
]);
// js
function (response) {
const data = JSON.parse(response);
}
推荐阅读
- django - Django:在电子邮件中使用正确的“url”标签?(一个完整的“http://...”标签。)
- python - 删除 INSTALLED_APPS 中的默认应用
- sql-server - 有没有办法忽略 SSIS 中的映射错误?
- python - 通过服务器上的 CGI 烧瓶提供 EMPTY_RESPONSE
- ios - 如何让 Rx 主题对添加到数组的新元素做出反应?
- c# - 注册表类。如果密钥存在
- javascript - 在 react native 中使用 Flatlists renderItem,如果我使用 Item 的值作为数组的索引,它是未定义的
- java - 我正在尝试生成一个随机的不同的 3 或 4 位数字
- angular - ng serve 在导入模块错误中运行...没有导出成员
- python - 您可以在 discord.py 命令中传递超过 1 个参数吗?