我在通过 jQuery Ajax 发送 $_POST
数据时遇到问题。我已经阅读了我能找到的关于此事的所有内容,但我仍然一无所获。我什至回到了非常基本的数据,但仍然一无所获,JSON 数据已经使用 JSON 验证站点进行了验证。在我的生活中,我无法echo
/print_r
/var_dump
或访问任何数据。
我的 JavaScript:
$('#updateJSON').click(function(){
var content = [{
"id": 21,
"children": [{
"id": 196
}, {
"id": 195
}, {
"id": 49
}, {
"id": 194
}]
}];
var someText = "someText";
$.ajax({
type: 'POST',
url: 'test.php',
data: {data: content},
cache: false,
success: function(){
alert("success");
console.log(content);
window.location = "test.php";
},
error:function(){
alert('ajax failed');
}
});
});
我的 PHP:
<?php
if(isset($_POST['data'])) {
$json = stripslashes($_POST['data']);
var_dump(json_decode($json, true));
} else {
echo "No Data";
}
?>
所以我得到了 alert("success")
,然后被重定向到 test.php
一次在 test.php
我得到回显“无数据”。
提前感谢您对此问题的任何帮助。
最佳答案
原因是您正在使用 window.location
重定向到 PHP 文件(没有任何发布数据)而不是在 test.php
上发布表单.
您应该在没有 ajax
的情况下发布表单在 test.php
.
或使用来自 test.php
的响应在你的success
功能。
$('#updateJSON').click(function(){
var content = [{
"id": 21,
"children": [{
"id": 196
}, {
"id": 195
}, {
"id": 49
}, {
"id": 194
}]
}];
var someText = "someText";
$.ajax({
type: 'POST',
url: 'test.php',
data: {data: content},
cache: false,
success: function(response){
alert("success");
console.log(response);
//window.location = "test.php";
},
error:function(){
alert('ajax failed');
}
});
关于php - AJAX POST JSON 到 PHP 问题 PHP 中没有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38684351/