前端使用的是vue,下面是axios的主要代码
methods: {
search: function () {
var params = {
content1: this.content1
}
this.$axios.post("http://127.0.0.1:8000/search/", params)
.then((response)=> {
console.log(response);
this.response1=response.data['content1']
})
.catch(function (error) {
console.log(error);
})
},
find: function () {
this.$axios.get("http://127.0.0.1:8000/find/", {
params: {
content2: this.content2
}
})
.then((response)=> {
console.log(response);
this.response2=response.data['content2']
})
.catch(function (error) {
console.log(error);
})
},
},
后端是django框架,代码如下
@csrf_exempt
def search(request):
post_content = json.loads(request.body, encoding='utf-8')['content1']
print(type(post_content))
print("post_content是:")
print(post_content)
return JsonResponse({'content1': 'post请求' + post_content * 2, 'msg': '错误信息'})
@csrf_exempt
def find(request):
find_content = request.GET.get('content2')
print("find_content是:")
print(type(find_content))
print(find_content)
return JsonResponse({'content2': 'get请求' + find_content * 3})
这里主要是新手对axios和前后端分离开发的学习