首页 >> js开发 >> jsdjango简单的前后端分离的数据传输实例 axiosjs大全
jsdjango简单的前后端分离的数据传输实例 axiosjs大全
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
前端使用的是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就是小编分享给大家的全部内容了,希望能给大家一个参考。
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就是小编分享给大家的全部内容了,希望能给大家一个参考。
相关文章:
- jsvue项目使用$router.go(-1)返回时刷新原来的界面操作js大全
- jsES2021系列之空值合并运算符 '??'js大全
- jsvue data对象重新赋值无效(未更改)的解决方式js大全
- jsVUE项目axios请求头更改Content-Type操作js大全
- jsVue-CLI 3 scp2自动部署项目至服务器的方法js大全
- js使用React-Router实现前端路由鉴权的示例代码js大全
- JavaScriptthree.js欧拉角和四元数的使用方法
- jsvuex中store存储store.commit和store.dispatch的用法js大全
- jsElement Input输入框的使用方法js大全
- js解决父组件将子组件作为弹窗调用只执行一次created的问题js大全