The front-end uses vue, and the following is the main code of axios
methods: { search: function () { var params = { content1: this.content1 } this.$("http://127.0.0.1:8000/search/", params) .then((response)=> { (response); this.response1=['content1'] }) .catch(function (error) { (error); }) }, find: function () { this.$("http://127.0.0.1:8000/find/", { params: { content2: this.content2 } }) .then((response)=> { (response); this.response2=['content2'] }) .catch(function (error) { (error); }) }, },
The backend is the django framework, the code is as follows
@csrf_exempt def search(request): post_content = (, encoding='utf-8')['content1'] print(type(post_content)) print("post_content is:") print(post_content) return JsonResponse({'content1': 'post request' + post_content * 2, 'msg': 'error message'}) @csrf_exempt def find(request): find_content = ('content2') print("find_content is:") print(type(find_content)) print(find_content) return JsonResponse({'content2': 'get request' + find_content * 3})
This is mainly for beginners to learn how to separate axios and front-end
Supplementary knowledge:ajax cannot obtain the request parameters in the backend, but the frontend has passed it
If you use ajax, if you use post to submit data, if the content-type is not set to application/x-www-form-urlencoded, the default mode text/plain; charset=utf-8.
In tomcat, special processing is made for the post submission method. If the submission method is post and content-type is not equal to application/x-www-form-urlencoded, the request parameters will not be parsed at the bottom of tomcat, and it will not be placed in the map of the requestparameter, so if you use (name) you cannot get the requested parameters.
In the browser network, the data submitted by the post submission method will be displayed under form date instead of request payload.
The above article "Django's simple front-end and back-end data transmission example" is all the content I share with you. I hope you can give you a reference and I hope you can support me more.