E
E
EVOSandru62015-04-13 09:33:09
JavaScript
EVOSandru6, 2015-04-13 09:33:09

How to generate url in ajax in yii?

Good afternoon,

Such a problem, I can not get data in

actionaddToCart($code, $count){
      ...
}


Ajax like this:
$.ajax({
                data: { code: product_code, count: qty },
                type: 'GET',
                url: '/cart/addToCart',
                success: function(msg){
                    alert( "Прибыли данные: " + msg );
                },
                error: function(){
                    alert('error!');
                }
            })


If in actionaddToCart I write simply

by the get method without data It alerts. I'm interested in 2 points: 1. how to form 2 parameters in ajax : data and url so that data can be sent to actionaddToCart using the Get method? 2. I also tried this: AJAX

echo 123;











$.ajax({
                data: { code: product_code, count: qty },
                type: 'POST',
                url: '/cart/addToCart',
                success: function(msg){
                    alert( "Прибыли данные: " + msg );
                },
                error: function(){
                    alert('error!');
                }
            })


PHP
actionaddToCart (){
if(!empty($_POST['code']) && !empty($_POST['count'])){
            echo 'Привет!';
}
}


Returned in the POST console website/cart/addToCart 400 (Bad Request) . Help with advice, how to accept data in action?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vit, 2015-04-13
@fornit1917

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},
});

E
Eugene, 2015-04-13
@Nc_Soft

Yii does not know how to accept post parameters as action arguments without dancing with a tambourine. Therefore, remove the arguments from function actionAddToCart()

S
Sergey, 2015-04-13
@butteff

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);
      		}	
      	}
    });
});

well, in your case, you need to replace the selectors with your own and the get method, not post

A
Alexander, 2015-04-13
@wiggle

Url header can be used

<?php
use yii\helpers\Url;
?>
<script>
var url = <?= Url::to(['coroller/action', 'get_param' => 100]); ?>
...
// Сам аякс запрос
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question