ThinkPHP + jquery easyui form 提交在IE兼容模式下变文件下载的解决办法

时间:2022-08-27 14:03:14

ThinkPHP + jquery easyui 1.4.1  做的一个登录页面,在chrome、FF等浏览器上是正常的,但是在IE兼容模式下,一提交Form就变成了下载文件了。


页面代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>BlueSky后台管理系统</title>
    <load href='__PUBLIC__/jquery-easyui-1.4.1/themes/default/easyui.css' />
    <load href='__PUBLIC__/jquery-easyui-1.4.1/themes/icon.css' />
    <load href='__PUBLIC__/jquery-easyui-1.4.1/jquery.min.js' />
    <load href='__PUBLIC__/jquery-easyui-1.4.1/jquery.easyui.min.js' />
    <load href='__PUBLIC__/jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js' />
</head>
<body>
<div class="easyui-dialog" title="登录" data-options="iconCls:'icon-tip',closable: false" style="width:420px;height:200px;padding:10px;">
    <div style="padding:10px 60px 20px 60px;">
        <form id="ff" class="easyui-form" method="post" data-options="novalidate:true" action="__APP__/Admin/Index/login">
            <table cellpadding="5">
                <tr>
                    <td>用户名:</td>
                    <td><input class="easyui-textbox" type="text" name="name" data-options="required:true,prompt:'手机/邮箱/呢称'" ></input></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><input class="easyui-textbox" type="password" name="password" data-options="required:true"></input></td>
                </tr>
            </table>
        </form>
        <div style="text-align:center;padding:5px">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()">登录</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()">清空</a>
        </div>
    </div>
</div>
<script>
    function submitForm(){
        $('#ff').form('submit',{
            onSubmit:function(){
                return $(this).form('enableValidation').form('validate');
            },
            success:function(data){
                var d = $.parseJSON(data);
                if(d.state==1){
                    window.location= d.url;
                }else{
                    $.messager.alert('提示:', d.msg,'error');
                }
            }
        });
    }
    function clearForm(){
        $('#ff').form('clear');
    }
    //回车提交表单
    $.parser.onComplete = function(){
        $(".textbox-text").keyup(function (e) {
            if (e.keyCode == 13) {
                submitForm();
            }
        });
    };
</script>
</body>
</html>


PHP Controller:

<?php
namespace Admin\Controller;
use Admin\Model\UserModel;
use Think\Controller;
class IndexController extends Controller {
    /*
     * 登录
     */
    public function login(){
        $user = M("user");
        $where["nickname"] = ":name";
        $where["email"] = ":name";
        $where["mobile"] = ":name";
        $where["_logic"] = "OR";
        $map["password"] = ":password";
        $map['_complex'] = $where;
        $bind[':name'] = I("post.name");
        $bind[':password'] = I("post.password");
        $list = $user->where($map)->bind($bind)->select();
        if(count($list) == 1){
            $currentUser = current($list);
            $currentUser["lasttime"] = date('Y-m-d H:i:s',time());
            session("user",$currentUser);
            //保存最后登录时间
            $user->field('lasttime,id')->save($currentUser);

            $data["state"] = 1;
            $data["url"] = U("Admin/Index/main");
            $this->ajaxReturn($data);
        }else{
            $data["state"] = 0;
            $data["msg"] = "用户名或密码不正确!";
            $this->ajaxReturn($data);
        }
    }
}
IE兼容模式点登录就变成下载文件了。
ThinkPHP + jquery easyui form 提交在IE兼容模式下变文件下载的解决办法

引起问题的原因应该是 easyui 1.4.1 不再支持IE6、7、8所致,经测试,解决问题的方法,是不用 ajaxReturn 方法返回数据,因为ajaxReturn 会在返回数据前添加 header 声明,我们直接用 echo或exit 之类的方法直接输出字符串就好了。

PHP Controller 修改如下:

<?php
namespace Admin\Controller;
use Admin\Model\UserModel;
use Think\Controller;
class IndexController extends Controller {    
    /*
     * 登录
     */
    public function login(){
        $user = M("user");
        $where["nickname"] = ":name";
        $where["email"] = ":name";
        $where["mobile"] = ":name";
        $where["_logic"] = "OR";
        $map["password"] = ":password";
        $map['_complex'] = $where;
        $bind[':name'] = I("post.name");
        $bind[':password'] = I("post.password");
        $list = $user->where($map)->bind($bind)->select();
        if(count($list) == 1){
            $currentUser = current($list);
            $currentUser["lasttime"] = date('Y-m-d H:i:s',time());
            session("user",$currentUser);
            //保存最后登录时间
            $user->field('lasttime,id')->save($currentUser);

            $data["state"] = 1;
            $data["url"] = U("Admin/Index/main");
            exit(json_encode($data));
        }else{
            $data["state"] = 0;
            $data["msg"] = "用户名或密码不正确!";
            exit(json_encode($data));
        }
    }

}