Answer the question
In order to leave comments, you need to log in
Why does AJAX return null or 0 using the POST method?
Good afternoon)
Who will tell you what is wrong? Having declared a variable with a value of 2, I send its value using ajax (to the db.php file), but 0 comes in.
Here is the function in the index.php file
$(document).ready(function(){
var limit = 2 ;
$("#btn-more").click(function(){
$.ajax({
url:"db.php",
type: "POST",
data: {limit: limit},
success:function(html) {
$("#res").html(html);
}
});
});
});
$data = $_POST['limit'];
echo $data;
Answer the question
In order to leave comments, you need to log in
Потому что в бади надо либо json отдать, либо запаковать данные в form data, urlencoded... короче в тупую в бади запроса написать объект js так не работает
Всё получилось в таком варианте
var obj = 3;
$("#btn-more").click(function(){
obj += 3;
$.ajax({
url:"db.php",
type: "POST",
data :"param=" +JSON.stringify(obj),
success:function(data) {
document.getElementById("res").innerHTML = data;
},
});
});
});
$data = json_decode($_POST['param']);
echo $data;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question