Javascript 从JQuery.post()恢复数据
我正在使用Javascript 从JQuery.post()恢复数据,javascript,php,jquery,post,Javascript,Php,Jquery,Post,我正在使用$.post()将数据推送到php文件中。但是当我对它进行var\u dump时,$\u POST是空的 JS脚本: $('#Notes').on("click", function (e) { e.preventDefault(); let $id = $(document).getUrlParam("varname"); let $text = $(this).attr('data'); let $notes = p
$.post()
将数据推送到php
文件中。但是当我对它进行var\u dump
时,$\u POST
是空的
JS脚本
:
$('#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
文件中:
$('#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中使用json\u encode()
来打印复杂对象。然后在JS中,您可以使用JSON.parse()
在POST
查询中还原对象或定义application/JSON
内容类型。比如:
//php
$json=json\U编码([
“post”=>$\u post,
“获取”=>$\u获取
]);
//js
功能(响应){
const data=JSON.parse(响应);
}
您可以用这种方式尝试
JQuery代码
$('#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) {
var data=$.parseJSON(data);
console.log('Data Id : ',data.id);
console.log('Data Name : ',data.notes);
})
.then(r =>{
//location.replace('../buckets/update_note.buckets.php');
})
}
PHP代码
echo json_encode($_POST);
同一个问题我回显了$json,结果是:“{”post:[],“get:[]}”同一个问题,我的php页面上的回显write[],是空的。