Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

数组中有对象avalon.param转后对象丢失 #10

Closed
wf123537200 opened this issue Dec 17, 2014 · 3 comments
Closed

数组中有对象avalon.param转后对象丢失 #10

wf123537200 opened this issue Dec 17, 2014 · 3 comments

Comments

@wf123537200
Copy link

image
以上是avalon的测试
image
以上是jq的。。
丢了数据了~~

@RubyLouvre
Copy link
Owner

我看一看

@RubyLouvre
Copy link
Owner

搞定

    function isDate(a) {
        return Object.prototype.toString.call(a) === "[object Date]"
    }
    if (!Date.prototype.toISOString) {
        (function() {

            function pad(number) {
                if (number < 10) {
                    return '0' + number;
                }
                return number;
            }

            Date.prototype.toISOString = function() {
                return this.getUTCFullYear() +
                        '-' + pad(this.getUTCMonth() + 1) +
                        '-' + pad(this.getUTCDate()) +
                        'T' + pad(this.getUTCHours()) +
                        ':' + pad(this.getUTCMinutes()) +
                        ':' + pad(this.getUTCSeconds()) +
                        '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) +
                        'Z';
            };
        }());
    }

    function paramInner(json, prefix, buffer) {
        prefix = prefix || ""
        for (var key in json) {
            if (json.hasOwnProperty(key)) {
                var val = json[key]
                var name = prefix ? prefix + encode("[" + key + "]") : encode(key)
                if (isDate(val)) {
                    buffer.push(name + "=" + val.toISOString())
                } else if (isValidParamValue(val)) {//如果是简单数据类型
                    buffer.push(name + "=" + encode(val))
                } else if (Array.isArray(val) || avalon.isPlainObject(val)) {
                    avalon.each(val, function(subKey, subVal) {
                        if (isValidParamValue(subVal)) {
                            buffer.push(name + encode("[" + subKey + "]") + "=" + encode(subVal))
                        } else if (Array.isArray(val) || avalon.isPlainObject(val)) {
                            paramInner(subVal, name + encode("[" + subKey + "]"), buffer)
                        }
                    })
                }
            }
        }
    }

    function isValidParamValue(val) {
        var t = typeof val; // 值只能为 null, undefined, number, string, boolean
        return val == null || (t !== 'object' && t !== 'function')
    }

        //将一个对象转换为字符串
       avalon. param = function(json) {
            if (!avalon.isPlainObject(json)) {
                return "";
            }
            var buffer = []
            paramInner(json, "", buffer)
            return buffer.join("&").replace(r20, "+")
        }

@wf123537200
Copy link
Author

真迅速,3q哈~~

发件人: 司徒正美 [mailto:notifications@github.com]
发送时间: 2014年12月17日 20:21
收件人: RubyLouvre/mmRequest
抄送: loveSed
主题: Re: [mmRequest] 数组中有对象avalon.param转后对象丢失 (#10)

Closed #10 #10 .


Reply to this email directly or view it on GitHub #10 (comment) . https://github.com/notifications/beacon/AE5JIPe5rIrVf7SdUcdBl01Es78awwppks5nYWy7gaJpZM4DJZDU.gif

RubyLouvre pushed a commit that referenced this issue Mar 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants