9

i hope somebody can help me, the array value is empty in the post.

$(function start() {

    c_all = new Array('#div { font-color:#ff0000; border:1px solid #00ff00; }', '#div_2 { font-color:#ff0000; }', '.line2 { font-color:#00ffff; }');

    css(c_all);

});


function css(x) {

    values = new Array();

    for (i = 0; i < x.length; i++) {
        c0_selector = '' + x[i].match(/^.*{/) + '';
        c0_selector = c0_selector.replace(/\s*/g, '');
        c0_selector = c0_selector.replace(/{/, '');

        x[i] = x[i].replace(/^.*{/, '');
        x[i] = x[i].replace(/}/, '');

        c0_arr = x[i].split(';');

        values['' + c0_selector + ''] = new Array();

        $('#log').append(''+c0_selector+'<br />');

        for (i2 = 0; i2 < c0_arr.length; i2++)
        {
            values[''+c0_selector+''][i2] = c0_arr[i2].split(':');
            $('#log').append(''+c0_arr[i2]+'<br />');  
        }

    }

    $.ajax({
            type: 'post',
            data: JSON.stringify(values),
            contentType: 'application/json',
            dataType: 'json'
    });


}

working example -> http://www.jsfiddle.net/V9Euk/448/

Thanks in advance! Peter

2 Answers 2

12

Try making values an Object, (like it should be in javascript for named keys).

var values = {};

Also, it is a really good idea to declare your variables with the var keyword, so you're not creating global variables.

Also, no need for '' + c0_selector + '' since you already have a String. Just do c0_selector.

Finished product logs the populated Object. http://www.jsfiddle.net/V9Euk/450/

Sign up to request clarification or add additional context in comments.

Comments

9

This is straight from the ECMAScript spec.

The abstract operation JA(value) serializes an array. It has access to the stack, indent, gap, and space of the invocation of the stringify method. The representation of arrays includes only the elements between zero and array.length – 1 inclusive. Named properties are excluded from the stringification. An array is stringified as an open left bracket, elements separated by comma, and a closing right bracket.

Basically any named properties are excluded from the result.

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.