标签: windowname

  • 使用 jQuery.windowName 进行 ajax 提交数据时的一个注意事项

    0.9.1 版本来说明,开头有这么几行代码:

    (function () { = || window.jQuery;
        var origAjax =.ajax,
            idx = 0;
        $.extend({
            ajax: function (s) {
                var remote = /^(?:\w+:)?\/\/([^\/?#]+)/,
                    data = '',
                    status = '',
                    requestDone = false,
                    xhr = null,
                    type = s.type.toUpperCase(),
                    ival = setTimeout(function () {}, 0),
                    onreadystatechange = null,
                    success = null,
                    complete = null,
                    localdom = remote.exec(s.url);
                if (s.windowname || (type === 'POST' & amp; & amp; localdom & amp; & amp; localdom[1] !== location.host)) {
    
                    // more code...
                } else {
                    return origAjax.apply(this, arguments);
                }
            }
        });
    })();
    

    留意第 11 行的判断条件,当不满足的时候将使用回普通的非跨域的 ajax 提交。

    那行的判断条件是:配置中存在着为 true 的 windowname 属性或是 post 类型的提交且存在着非同域的有效提交地址。

    所以当想确保使用跨域 ajax 提交时,需要确保进入 if 分支才行,不然服务器那边用跨域的方式返回数据,js 这边却认为是非跨域的,结果就报错了。

    由于测试的时候可能忘记了使用不同的域,所以最保险的方法是在 .ajax 方法中显式加上 windowname: 1

    今天被测试机上的这个问题搞了 2 个小时,找到真相后欲哭无泪。

    使用jquery windowname插件实现跨域提交的几个要点也提到些值得注意的地方。