Internet Explorer中的Ajax和JSON响应错误(适用于所有其他浏览器)

Wal*_*ker 6 javascript php jquery internet-explorer json

出于某种原因,IE要求我们下载文件而不是将其作为ajax运行.这适用于IE以外的所有浏览器.我试着弄乱它没有运气的标题.

该函数抓取表单数据然后发布它的响应是可以是在页面上要更新的任意数量项目的数组.

它不应该是文件,它假设只是一个json响应.

PHP

header('Content-type: application/json');

$error = "The Email and Password you entered could not be resolved.";
$elements[0]['target'] = '.error_report';
$elements[0]['action'] = 'inside';
$elements[0]['data'] = '<p>'.$error.'</p>';
$this->output->set_output(
  json_encode(array("elements" => $elements))
);
Run Code Online (Sandbox Code Playgroud)

使用Javascript

$(document).ready(function () {
    jQuery.ajaxSetup({
        cache: false,
        dataType: 'json',
        error: function () {
            alert("Request was not successful. Please try again shortly.");
        }
    });

    $(document).ajaxSuccess(function (e, xhr, settings) {
        var response = xhr.responseText;
        if (settings.dataType != 'json') {
            return;
        };

        try {
            response = jQuery.parseJSON(response);
        } catch (e) {
            alert(e);
            return;
        }

        if (response.elements instanceof Array) {
            var reqs = ['target', 'action'];
            var valid = true;
            for (var i=0;i<response.elements.length;i++) {
                var cur = response.elements[i];
                var sel;

                for (var j=0;j<reqs.length;j++) {
                    if (typeof cur[reqs[j]] !== "string") {
                        valid = false;
                        break;
                    };
                };

                if (!valid) {
                    continue;
                };

                sel = $(cur.target);
                switch (cur.action) {
                    case "inside":
                        sel.html(cur.data);
                    break;
                    case "instead":
                        sel.replaceWith(cur.data);
                    break;
                    case "remove":
                        sel.remove();
                    break;
                    case "refreshPage":
                        window.location.reload();
                    default:
                        if (typeof sel[cur.action] === "function") {
                            sel[cur.action](cur.data);
                        }; // else continue
                    break;
                };
            };
        };


            // Dispatch the AJAX request, and save it to the data object so that
            // is can be referenced and cancelled if need be.

            self.data('ajaxify.xhr', jQuery.ajax({
                url: this.action,
                type: 'post',
                dataType: options.dataType,
                data: (beforeSubmitIsJquery ? beforeSubmitResult.serialize()
                                            : self.serialize()),
                success: function (/**/) {
                    cleanup();

                    options.onSuccess.apply(that, arguments);
                },
                error: function (/**/) {
                    cleanup();

                    options.onError.apply(that, arguments);
                },
                complete: function (/**/) {
                    options.onComplete.apply(that, arguments);
                }
            }));
Run Code Online (Sandbox Code Playgroud)

jco*_*and 2

好吧,我问是因为您描述的行为具有双重发布的所有特征,这是由事件处理程序启动 ajax 请求,然后发生“本机”浏览器表单提交而引起的。如果我是你,我会额外确保你的事件处理程序要么返回“false”,要么调用“preventDefault”,或者两者都有:-) \xe2\x80\x93 Pointy 1 小时前

\n\n

我的后续:由于 IE 忽略了 PreventDefault,请尝试使用 return false; 预防默认后...

\n\n

供其他开发人员将来参考:公共库倾向于执行此操作的方式是,它们通常会使用两种方法(preventDefault() 和 return false;)编写一个块,因为这会告诉每个主要浏览器停止处理该事件,根据他们听的内容。这对于旧版 IE 浏览器更为重要。

\n\n

不管怎样,很高兴我们能提供帮助。

\n