Answer the question
In order to leave comments, you need to log in
How to generate url in ajax in yii?
Good afternoon,
Such a problem, I can not get data in
actionaddToCart($code, $count){
...
}
$.ajax({
data: { code: product_code, count: qty },
type: 'GET',
url: '/cart/addToCart',
success: function(msg){
alert( "Прибыли данные: " + msg );
},
error: function(){
alert('error!');
}
})
echo 123;
$.ajax({
data: { code: product_code, count: qty },
type: 'POST',
url: '/cart/addToCart',
success: function(msg){
alert( "Прибыли данные: " + msg );
},
error: function(){
alert('error!');
}
})
actionaddToCart (){
if(!empty($_POST['code']) && !empty($_POST['count'])){
echo 'Привет!';
}
}
Answer the question
In order to leave comments, you need to log in
yii2? If so, then the CSRF token is checked there in the post-requests.
Or write public $enableCsrfValidation=false in the controller (but this is not very good, because it adds vulnerabilities). Or manually add a parameter with a csrf token to the Ajax request parameters (data) in javascript:
var csrfToken = $('meta[name="csrf-token"]').attr("content");
$.ajax({
.....
data: {param1: param1, _csrf : csrfToken},
});
Yii does not know how to accept post parameters as action arguments without dancing with a tambourine. Therefore, remove the arguments from function actionAddToCart()
I do like this in yii2:
$('#user-profile').on('beforeSubmit', function(){
var url = $('#user-profile').attr('action');
var data = $('#user-profile').serialize();
$.ajax({
type: "POST",
url: url,
data: data,
success:function(data) {
if (typeof(data)!='undefined' && data !='') {
alert(data);
}
}
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question