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

标签 php javascript 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))
);

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);
                }
            }));

最佳答案

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

我的后续行动:由于 IE 忽略了 preventDefault,请尝试使用 return false;在 preventDefault 之后 ...

为了将来供其他开发人员引用:公共(public)库倾向于这样做的方式是它们通常会使用两种方法(preventDefault() 和 return false;)编写一个 block ,因为这会告诉每个主要浏览器停止工作他们听的事件。这对于旧版 IE 浏览器更为重要。

无论如何,很高兴我们能提供帮助。

关于php - Internet Explorer 中的 Ajax 和 JSON 响应错误(适用于所有其他浏览器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3613387/

相关文章:

php - 关于回溯控制动词的信息

javascript - 模态对话框中 ui-grid 的大小调整问题

jquery - 为什么标签内容总是在 materializecss 中可见

javascript - jQuery 不能与 RequireJS 一起使用

php - 如何将 PHP 代码块放入 PHPDoc DocBlock

php - 防止数据库被外部访问

javascript - Vuex 2.0 Dispatch 与 Commit

javascript - 无法更改动态添加元素的属性

javascript - 为每个列表元素生成不同的、随机颜色的 DIV

php - 扩展的 mysqli 类中的慢父::__construct