2

I'm new to javascript and jquery. I have the following situation. I'm serializing an edited row of a table and sending it to the server via ajax.

I have to pass the original data (before editing) and the final data (edited row) to the server.

Passing each of them separately is working well, but when I put them together something strange is happening. It's probably something trivial for those who understand javascript well.

The "sending code" is:

$.ajax({
        data: data,
        dataType: 'json',
        ...
       });

when I get the original row data or the edited row data as:

data = tr.find(':input').serializeArray();

I get a normal dict in request.POST, like this:

<QueryDict: {u'Type': [u'CNAME'], u'Name': [u'domain_2'], Ttl': [u'500'], u'Value': [u'domain_1']}>

But when I try to get both original and new data like this:

original_data = tr.find(':input').serializeArray();
new_data = tr.find(':input').serializeArray();
var data = {}
data.original_data = original_data
data.new_value = new_data

This just does not work. I get something like this:

<QueryDict: {u'new_value[1][name]': [u'Type'], u'new_value[3][name]': [u'Ttl'], u'new_value[2][value]': [u'domain_1'], u'original_data[2][name]': [u'Value'], u'new_value[2][name]': [u'Value'], u'original_data[0][name]': [u'Name'], u'original_data[3][value]': [u'300'], u'original_data[1][value]': [u'CNAME'], u'new_value[3][value]': [u'500'], u'original_data[0][value]': [u'domain_2'], u'new_value[1][value]': [u'CNAME'], u'original_data[3][name]': [u'Ttl'], u'new_value[0][name]': [u'Name'], u'original_data[2][value]': [u'domain_1'], u'original_data[1][name]': [u'Type'], u'new_value[0][value]': [u'domain_2']}>

I was hoping to get

<QueryDict: {'original_data': {u'Type': [u'CNAME'], u'Name': [u'domain_2'], Ttl': [u'500'], u'Value': [u'domain_1']}, 'new_value': {u'Type': [u'CNAME'], u'Name': [u'domain_2'], Ttl': [u'500'], u'Value': [u'domain_1']}>>

How can I achieve this?

2 Answers 2

1

The solution was to use encodeURIComponent(JSON.stringify(data)) instead of data only.

As I'm using django, I also used dajaxice, so I actually added 'argv='+encodeURIComponent(JSON.stringify(data)), so Dajaxice looks for a key called 'argv' and gets the value to me.

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

Comments

0

request.POST is for data sent as normal form-encoded. You're not sending that, you're sending a raw JSON string. To access that in the view, use request.body.

1 Comment

how do I encode data in request.POST correctly. in request.body I have: 'new_data%5B0%5D%5Bname%5D=Name&new_data%5B0%5D%5Bvalue%5D=domain_2&new_data%5B1%5D%5Bname%5D=Type&new_data%5B1%5D%5Bvalue%5D=CNAME&new_data%5B2%5D%5Bname%5D=Value&new_data%5B2%5D%5Bvalue%5D=domain_1&new_data%5B3%5D%5Bname%5D=Ttl&new_data%5B3%5D%5Bvalue%5D=500&original_data%5B0%5D%5Bname%5D=Name&original_data%5B0%5D%5Bvalue%5D=domain_2&original_data%5B1%5D%5Bname%5D=Type&original_data%5B1%5D%5Bvalue%5D=CNAME& ... (I had to truncate the data, becasue of limits of chars) request.POST is the correct structure to access

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.