Operating within the Yii2 framework I have html form buttons that will toggle a certain feature. Using JS I intercept the submit action and execute an ajax post to r?=reports/sales
that should, in theory, call the actionSales
method in my ReportsController
. However, this is not the case.
What is happening instead is the ajax call returns an error with status 302
and subsequently redirects me to localhost
.
I have been searching the net for answers to this and I've come up with nothing but more questions.
I'd really appreciate it if someone could lead me in the right direction to a possible solution or a better understanding of why this is happening.
I have also included the response headers received in the ajax error callback.
snippet of sales.php view where my Yii2 generated html form buttons are located
<div class="block-options pull-right">
<div class="btn-group">
<a href="javascript:void(0)" class="btn btn-effect-ripple btn-default dropdown-toggle enable-tooltip" data-toggle="dropdown" title="" style="overflow: hidden; position: relative; top: -15px;" data-original-title="Options" aria-expanded="false"><span class="btn-ripple animate" style="height: 34px; width: 34px; top: -2px; left: -0.953125px;"></span><i class="fa fa-chevron-down"></i></a>
<ul class="dropdown-menu dropdown-menu-right">
<?php
if ($showGrid) {
?>
<li>
<?= Html::beginForm(['reports/sales'], 'post', ['class' => 'sale-grid-view-choice']); ?>
<?= Html::submitButton('Show Graph <i class="fa fa-bar-chart-o pull-right"></i>', ['class' => 'btn btn-square btn-default clearBtn', 'name' => 'showGrid']) ?>
<?= Html::hiddenInput('showGrid', 'false',['class'=>'show-grid-value']); ?>
<?= Html::endForm() ?>
</li>
<?php
} else {
?>
<li>
<?= Html::beginForm(['reports/sales'], 'post', ['class' => 'sale-grid-view-choice']); ?>
<?= Html::submitButton('Show Table <i class="fa fa-table pull-right"></i>', ['class' => 'btn btn-square btn-default clearBtn', 'name' => 'hideGrid']) ?>
<?= Html::hiddenInput('showGrid', 'true',['class'=>'show-grid-value']); ?>
<?= Html::endForm() ?>
</li>
<?php
}
?>
<li class="divider"></li>
<li>
<?= Html::beginForm(['reports/sales'], 'post', ['class' => 'sale-grid-view-choice']); ?>
<?= Html::submitButton('Reset <i class="fa fa-mail-reply fa-fw pull-right"></i>', ['class' => 'btn btn-square btn-default clearBtn', 'name' => 'hideGrid']) ?>
<?= Html::hiddenInput('showGrid', 'false',['class'=>'show-grid-value']); ?>
<?= Html::endForm() ?>
</li>
</ul>
</div>
</div>
JavaScript code executing ajax call
$(document).on("submit", ".sale-grid-view-choice", function(e) {
e.preventDefault();
var showGrid = $(this).find(".show-grid-value").val() == "true" ? true : false;
if (showGrid) {
$("#widget-chart-classic").hide();
$("#widget-grid-classic").removeClass("hide");
} else {
$("#widget-chart-classic").show();
$("#widget-grid-classic").hide();
}
$.ajax({
type: "post",
cache: false,
data: {
"showGrid": showGrid
},
url: "r?=reports/sales",
success: function(sessionVariable) {
console.log("Successfully updated grid view session variable to", sessionVariable);
},
error: function(e) {
console.log("Ajax call to update grid view session variable could not be processed", e);
}
});
})
Output from calling e.getAllResponseHeaders()
in ajax's error: function(e){}
callback
"Pragma: no-cache
Date: Sat, 25 Mar 2017 20:24:00 GMT
X-Debug-Duration: 136
X-Redirect: http://localhost/
X-Powered-By: PHP/7.1.1
X-Debug-Link: /debug/default/view?tag=58d6d1e08e8c8
Content-Type: text/html; charset=UTF-8
Cache-Control: no-store, no-cache, must-revalidate
Connection: Keep-Alive
X-Debug-Tag: 58d6d1e08e8c8
Keep-Alive: timeout=5, max=100
Content-Length: 0
Server: Apache/2.2.31 (Unix) mod_wsgi/3.5 Python/2.7.13 PHP/7.1.1 mod_ssl/2.2.31 OpenSSL/1.0.2j DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.9 Perl/v5.24.0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
"
UPDATE
After changing my URL from r?=reports/sales
to ?r=reports/sales
, as well as having tried Url::to("/reports/sales")
method as suggested, I got exactly the same result as before: the ajax returns a 302
status error with exactly the same output for the response headers as before.
Anything else I could be missing?
UPDATE 2
It seems I am now also being redirected to localhost
after refreshing the page.