7

I'm trying to pass an array to json.stringify but the returned value is coming back empty.

JSON.stringify({ json: data }) // returns `{"json":[]}`

And here would be the contents of data:

data[from] = "[email protected]"
data[to] = "[email protected]"
data[message] = "testmessage"

jquery:

function SubmitUserInformation($group) {
    var data = {};
    data = ArrayPush($group);
    $.ajax({
        type: "POST",
        url: "http://www.mlaglobal.com/components/handlers/FormRequestHandler.aspx/EmailFormRequestHandler",
        data: JSON.stringify({ json: data }),
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        cache: false,
        success: function (msg) {
            if (msg) {
                $('emailForm-content').hide();
                $('emailForm-thankyou').show();
            }
        },
        error: function (msg) {
            form.data("validator").invalidate(msg);
        }
    });
}

function ArrayPush($group) {
    var arr = new Array();
    $group.find('input[type=text],textarea').each(function () {
        arr[$(this).attr('id')] = $(this).val();
    });
    return arr;
}

2 Answers 2

5
data = ArrayPush($group);

Is re-assigning data to be an array, so all your expando properties are not being stringified.

Inside your ArrayPush method, change

var arr = new Array();

to

var obj = { };
Sign up to request clarification or add additional context in comments.

Comments

2

arr should be declared as an object inside ArrayPush method because you are not using it like an array. Also inside the function you can just use this.id and this.value you don't have to create the jQuery object. Try this

function ArrayPush($group) {
    var arr = {};
    $group.find('input[type=text],textarea').each(function () {
        arr[this.id] = this.value;
    });
    return arr;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.