前端使用的是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);

})

},

},
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})
@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和前后端分离开发的学习补充知识:ajax在后端获取不到请求参数,但是前端已经传递过去了补充知识:补充知识:ajax在后端获取不到请求参数,但是前端已经传递过去了使用ajax如果使用的是post方式提交数据,如果不设置content-type为application/x-www-form-urlencoded的话,默认的模式text/plain;charset=utf-8。在tomcat中对于post提交方式又做了特殊的处理如果提交方式为post而content-type又不等于application/x-www-form-urlencoded,在tomcat底层是不会去解析请求参数的,也不会放到requestparameter的map中,因此使用request.getParameter(name)也就获取不到请求的参数了。在浏览器中network中,一般post提交方式提交的数据也是会显示在form date下,而不是request payload下。以上这篇django简单的前后端分离的数据传输实例 axios就是小编分享给大家的全部内容了,希望能给大家一个参考。