$this->error()无法跳转的坑!

因为想做一个表单提交后发现某错误后跳转出去后台的功能,所以用到了$this->error($msg,$url),结果发现跳转完全不起作用。但是$this->success($msg,$url)跳转却可以。
看了看thinkphp官方的代码,没有发现自己传递参数时有啥错误。
代码在thinkphp/traits/controller/jump.php中。
/**
* 操作错误跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
{
$type = $this->getResponseType();
if (is_null($url)) {
$url = $this->app['request']->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ('' !== $url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app['url']->build($url);

}

$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];

if ('html' == strtolower($type)) {
$type = 'jump';
}

$response = Response::create($result, $type)->header($header)->options(['jump_template' => $this->app['config']->get('dispatch_error_tmpl')]);
throw new HttpResponseException($response);
}

然后再看thinkphp文档,发现有一句
error方法会自动判断当前请求是否属于Ajax请求,如果属于Ajax请求则会自动转换为default_ajax_return配置的格式返回信息。 success在Ajax请求下不返回信息,需要开发者自行处理。
围绕这个找了半天,发现能力有限,确实翻找不到,最终在dolphin.js260行左右找到了代码。
tips(msg, 'danger');
对表单提交时候的$this->error();有效,于是在下面加了一句。
if(res.url){
setTimeout(function(){
parent.location.href = res.url;
},res.wait*1000)
}
这下可以实现了,但是其实根本的问题没能解决,也算是个BUG吧,请官方看看是哪里的问题。

0 个评论

要回复文章请先登录注册