[英]$_POST won't receive ajax
AJAX works fine, but $_POST does not have a value. AJAX可以正常工作,但是$ _POST没有值。
What I have tried: 我尝试过的
$data = json_decode(file_get_contents('php://input'), true);
& $post = json_decode($data);
$post = json_decode($data);
into storecart.php 'jCart=' + jData'
'jCart=' + jData'
cart
is an array of objectscart
是一组对象
Javascript: Javascript:
$(document).ready(function(){
$("#showcart").click(function(event){
event.preventDefault();
showcart();
url = 'cart.php';
$(location).attr("href",url);
});
});
function showcart(){
var jData = JSON.stringify(cart);
$.ajax({
url:"storecart.php",
type:"post",
data: {jCart : jData},
datatype: "json",
success: function(data){
console.log("SUCCESS")
console.log(jData);
},
error: function(data){
console.log("REDO")
}
});
}
storecart.php: storecart.php:
<?php
if(isset($_POST['jCart'])){
echo "Right";
}else{
echo "Wrong";
}
?>
How do get the $_POST
to accept the json.stringify
? 如何获得
$_POST
接受json.stringify
?
SOLUTION: 解:
SOLVED: 解决了:
All i did was add a form that has a hidden value 我所做的就是添加一个具有隐藏值的表单
<form id = "postform" action = "cart.php" method = "post">
<input type = "hidden" id="obj" name="obj" val="">
<input type = "submit" value = "Show Cart" id = "showcart">
</form>
In the Javascript: 在Javascript中:
$(document).ready(function(){
$("#showcart").click(function(){
var json = JSON.stringify(cart)
$('#obj').val(json);
$('#obj').submit();
});
});
Thank you for everyone that has answered but hope this helps. 谢谢所有回答的人,希望对您有所帮助。
$(document).ready(function(){
var data = {one:"one", two:"two", three:"three"};
var jsonData = JSON.stringify(data);
$("#clickme").click(function() {
$.ajax({
url:"demo.php",
type:"POST",
data:{cart:jsonData},
success:function(response){
console.log(response);
}, error:function(err) {
console.log(err);
}
})
});
});
PHP 的PHP
<?php
if(isset($_POST['cart'])){
echo "Right";
}else{
echo "Wrong";
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.