Javascript 如何将JS对象作为JSON正确发送?

Javascript 如何将JS对象作为JSON正确发送?,javascript,json,Javascript,Json,我有以下JS对象: json = { "category_id": category, "subcategory_id": subcategory, "offer_type": type_offer, "features": [] }; 我尝试以类似JSON的方式发送此对象: $.ajax({ t

我有以下JS对象:

json =
            {
                "category_id": category,
                "subcategory_id": subcategory,
                "offer_type": type_offer,
                "features": []
            };
我尝试以类似JSON的方式发送此对象:

$.ajax({
            type: 'POST',
            url: '/add',
            data: json,
            success: function (data) {
            },
            contentType: "application/json",
            dataType: 'json'
        });
是这样吗?或者我需要做一些准备之前

现在我使用这部分代码:

formObj = $("#form_add").serialize();

var json = {};

var wrapperObj = {json: json, form: formObj};

    $.ajax({
                type: 'POST',
                url: '/add',
                data: JSON.stringify(wrapperObj),
                success: function (data) {
                   // TODO
                },
                contentType: "application/json",
                dataType: 'json'
            });

这条路对吗?当我将两个对象打包在一个对象中并在
stringify

之后时,需要使用JSON.stringify使其成为有效的JSON

$.ajax({
            type: 'POST',
            url: '/add',
            data: JSON.stringify(json),
            success: function (data) {
            },
            contentType: "application/json",
            dataType: 'json'
        });

您需要使用JSON.stringify使其成为有效的JSON

$.ajax({
            type: 'POST',
            url: '/add',
            data: JSON.stringify(json),
            success: function (data) {
            },
            contentType: "application/json",
            dataType: 'json'
        });

我认为您应该首先使用JSON.stringify:

$.ajax({
            type: 'POST',
            url: '/add',
            data: JSON.stringify(json),
            success: function (data) {
            },
            contentType: "application/json",
            dataType: 'json'
        });

我认为您应该首先使用JSON.stringify:

$.ajax({
            type: 'POST',
            url: '/add',
            data: JSON.stringify(json),
            success: function (data) {
            },
            contentType: "application/json",
            dataType: 'json'
        });
您也可以使用

您也可以使用


好的,它能工作吗?你能看到更新的问题吗?好的,它能工作吗?你能看到更新的问题吗