About the data format when form or Ajax sends data to the background, formajax

Source: Internet
Author: User

About the data format when form or Ajax sends data to the background, formajax

Recently I was working on an asset management system project. One part of the project is the data collected by the client to the server. The server is written by Django, and the client needs to use the rrequests module to simulate sending requests.

Assume that the sent data is as follows:

Data = {'status': True, 'content': {'k1 ': 'xxxx', 'k2': 'xxxx '}}

Is a dictionary, and then through requests. when post (url = url, data = data) is sent, you will find that the data received on the server is like this, <QueryDict >:{ ['status', 'content']}

What happened? Why did we get only the key but not the values? Since we can get the key, it means Django is okay.

The problem must be in sending data through requests. post.

Okay, the preparations are complete.

Think about how ajax sends data.

$. Ajax ({

Url: 'xxxx ',

Data :{

K1: 1,
K2: 'abc ',
K3: [1, 2, 3, 4, 's']

}

})

You can only send these three formats. No dictionary is available. If you want to send a dictionary, you can convert the dictionary into a string format using JSON. stringfy ({'k1 ': 'xxx '})

The same is true for form forms, while requests. post is an imitation form submission.

If the problem is found, let's analyze the process of sending a post request through http.

The data sent by ajax is a dictionary in the form of a key-value pair. During the http post request, the key-value pair is converted

K1 = xxx & k2 = xxx, with a request header:

Content-type: application/x-www-form-urlencoded

This request header corresponds to the format k1 = xxx & k2 = xxx in a one-to-one manner. As long as this format is sent, this request header must be included in the background request. POST will parse this format based on this request header and restore it to the previous dictionary format. In addition, this format and the request header are both default. If we do not modify the format, we can directly send the format mentioned at the beginning of the article, and the server cannot parse it, so we only get the dictionary key, value not obtained

However, in this case, we want to send JSON instead of the format. stringfy ({'k1 ': 'xxx'}) is a json string. ajax can customize the request header:

$. Ajax ({

Url: 'xxxx ',

Headers: {'content-type': 'application/json '},

Data: JSON. stringfy ({k1: 1, k2: 'abc', k3: [1, 2, 4, 's']})

})

In this way, the backend parses the received data (A json string) based on json)

However, you cannot use request. POST because request. POST parses data in the format of k1 = xxx & k2 = xxx by default,

You should use request. body to get the data, and use json. loads to get the dictionary.

 

By default, form forms, ajax, and requests. post are all processed in this way.

Therefore, in this project, we need to use requests to customize this request header, which is easy to write.

Requests. post (url = url, json = data), just change the original data keyword parameter to the json keyword parameter.

What is done internally is: 1. serialize data, 2. Carry a request header 'content-type': 'application/json'

In this way, decode the data obtained by the server and decode it. In json. loads, it is a dictionary we are familiar.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.