0

如果我的 jquery 序列化发布以下内容:

{"PurchaseOrders":[{"Id":"8a4c70a63db7b320013dc35247960095","Version":124,"Context":{"RealmId":"1","CustomerId":"111","ProjectId":"2222","Operation":null}

如何排除

{"PurchaseOrders":[

从我的帖子中,同时仍然提交它包含的所有内容?

我目前正在使用此脚本进行序列化: https ://github.com/tlowrimore/jquery.serialize-object/blob/master/jquery.serialize-object.js

(function($) {
    var methods = {
        setValue: function(path, value, obj) {
            if(path.length) {
                var attr = path.shift();
                if(attr) {
                    obj[attr] = methods.setValue(path, value, obj[attr] || {});
                    return obj;
                } else {
                    if(obj.push) {
                        obj.push(value);
                        return obj;
                    } else {
                        return [value];
                    }
                }
            } else {
                return value;
            }
        }
    };

    $.fn.serializeObject2 = function() {
        var obj     = {},
            params  = this.serializeArray(),
            path    = null;

        $.each(params, function() {
            path = this.name.replace(/\]/g, "").split(/\[/);
            methods.setValue(path, this.value, obj);
        });

        return obj;
    };
})(jQuery);

$(document).ready(function() {

    $("#btn-add-po-submit").click(function(e) {

        e.preventDefault();

        var formData = $("#form-add-po").serializeObject2();
        var url = $("#form-add-po").attr("action");

        console.log(url);

        $.ajax({
            type: 'POST',
            contentType: "application/json",
            url: url,
            dataType: 'json',
            data: JSON.stringify(formData),
            error : function(jqXHR, textStatus, errorThrown) {
                console.error(textStatus);
            },
            success : function(data, textStatus, jqXHR) {
                console.log(textStatus);

            }
        });
        /*$('#result').text(JSON.stringify($('form').serializeObject()));
        return false;*/
    });
4

1 回答 1

0

JSON 对象应该是某个变量的一部分。如果它是分开的,var obj那么您可以使用obj['PurchaseOrders']or获取 PurchaseOrders 数组obj.PurchaseOrders

于 2013-04-01T02:12:22.803 回答