jquery.ajax,post等方法在firefox与其他浏览器的区别

2015-10-30 13:57

客户端用jquery.post和jquery.ajax请求内容,asp.net mvc服务器端用Content返回文本内容时候,firefox和chrome,ie等浏览器的结果不同。firefox返回的是XmlDocument对象,chrome和ie都是字符串。

比如

                $.post('@Url.Action("EditAnswer")'
, { QuestionId: $div.get(0).id.substr(2), Content: $div.find('.wan_s').val(), CreateUser: 0 }
, function (data) {
                    if (data)
                        alert(data);
                    else
                        reload();
                });

服务器端代码

        public ActionResult EditAnswer(AnswerAddRequest request)
        {
            if (LoginUser == null)
                return Content("请先登录");
            ErrorCodeEnum? error;
            request.CreateUser = LoginUser.UNID;
            if (new AnswerBLL().EditAnswer(request, out error) > 0 || error == null)
                return Content("");
            return Content(error.Value.GetDescription());
        }

ie和chrome都正常。firefox下,不论用户是否已经登陆,客户端都会出现内容“[object XMLDocument]”的alert对话框。

解决办法 jquery.post和jquery.ajax都带上dataType参数,比如dataType:'html'。上面的代码修改为

            $div.find('a.jixu_r').show().click(function () {
                $.post('@Url.Action("EditAnswer")'
, { QuestionId: $div.get(0).id.substr(2), Content: $div.find('.wan_s').val(), CreateUser: 0 }
, function (data) {
                    if (data)
                        alert(data);
                    else
                        reload();
                },'html');
            });

就ok了

点击:loading..
收藏到:中国收客网
评论