【发布时间】:2017-08-18 16:49:56
【问题描述】:
在 Yii2 框架内操作我有 html 表单按钮,可以切换某个功能。使用 JS,我拦截了提交操作并执行了一个 ajax 发布到 r?=reports/sales,理论上应该在我的 ReportsController 中调用 actionSales 方法。然而,事实并非如此。
发生的事情是 ajax 调用返回状态为302 的错误,随后将我重定向到localhost。
我一直在网上搜索这个问题的答案,但我想出了更多问题。
如果有人能引导我找到可能的解决方案或更好地理解为什么会发生这种情况,我将不胜感激。
我还在 ajax 错误回调中包含了收到的响应标头。
我的 Yii2 生成的 html 表单按钮所在的 sales.php 视图的 sn-p
<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>
执行 ajax 调用的 JavaScript 代码
$(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);
}
});
})
在 ajax 的 error: function(e){} 回调中调用 e.getAllResponseHeaders() 的输出
"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
"
更新
在将我的 URL 从r?=reports/sales 更改为?r=reports/sales 之后,并按照建议尝试了Url::to("/reports/sales") 方法,我得到了与以前完全相同的结果:ajax 返回一个302 状态错误,完全相同像以前一样输出响应标头。
还有什么我可能遗漏的吗?
更新 2
刷新页面后,我现在似乎也被重定向到localhost。
【问题讨论】:
标签: php jquery ajax yii2 http-status-code-302