jQuery encodes my data in a different way on server than on localhost?

138 views Asked by At

I've a function like this:

function flush_changes() {
            jQuery('#save-changes').replaceWith('<span id="save-changes">Saving..</span>');
            var changes = new Array();
            for (var i=0; i<edited_users.length; i++) {
                changes.push({
                    id: edited_users[i],
                    first_profession: jQuery('#user_first_profession_' + edited_users[i]).val(),
                    second_profession: jQuery('#user_second_profession_' + edited_users[i]).val()
                });
            }
            jQuery.post("${tg.url('/users/admin_user_professions/save')}",
                        {
                            changed_users: changes,
                            num_of_changed_users: changes.length
                        },
                        function(data) {
                            if (data.result == 'OK') {
                                location.href = location.href;    
                            } else {
                                alert('Error while saving: ' + data.reason);
                            }
                        }, 'json');
        }

When it gets called, if i run the application from localhost i can see my data sent correctly, while when i run the application on my production server i see (using the webkit inspector) the data passed as follows:

changed_users:[object Object]
num_of_changed_users:1

Why on production server i get "object Object"? The jQuery library is the same on the two environments.

Thanks in advance!

EDIT Here is the output on the inspector on localhost

changed_users%5B0%5D%5Bid%5D:314
changed_users%5B0%5D%5Bfirst_profession%5D:5
changed_users%5B0%5D%5Bsecond_profession%5D:6 num_of_changed_users:1
1

There are 1 answers

0
albanx On

you are sending a json data to server, you must see how your server script get this data and how it decodes them. For example on php I use json_decode for converting json to arrays or objects, may in your production server this process is automatic (by configuration). Also remember to use encodeuricomponent on text when sending data to server, in your case I would do this:

.......
first_profession: encodeURIcomponent(jQuery('#user_first_profession_'+edited_users[i]).val()),
.......

(no need decode of uri on server side is done automatically)