django+vue實(shí)現(xiàn)跨域的示例代碼
版本
Django 2.2.3
Python 3.8.8
djangorestframework 3.13.1
django-cors-headers 3.11.0
django實(shí)現(xiàn)跨域
說(shuō)明:此處方法為后端解決跨越,即django端解決跨越。
1. 安裝 django-cors-headers 庫(kù)
pip install django-cors-headers
2. 修改項(xiàng)目配置文件 項(xiàng)目/settings.py
...
# Application definition
INSTALLED_APPS = [
'django.contrib.staticfiles',
'corsheaders', # 添加:跨域組件
'rest_framework', # 添加:DRF框架
'home', # 子應(yīng)用
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware', # 添加:放首行(放其他行未測(cè)試)
'django.middleware.security.SecurityMiddleware',
...
# CORS組的配置信息
CORS_ORIGIN_WHITELIST = (
'http://127.0.0.1:8080',
# 這里需要注意: 1. 必須添加http://否則報(bào)錯(cuò)(https未測(cè)試) 2. 此地址就是允許跨域的地址,即前端地址
)
CORS_ALLOW_CREDENTIALS = True # 允許ajax跨域請(qǐng)求時(shí)攜帶cookie至此django端配置完畢
3. 前端vue使用axios訪(fǎng)問(wèn)后端django提供的數(shù)據(jù)接口,安裝axios
npm install axios -S
4. 前端vue配置axios插件,修改src/main.js
... import axios from 'axios'; // 添加: 導(dǎo)入axios包 // axios.defaults.withCredentials = true; // 允許ajax發(fā)送請(qǐng)求時(shí)附帶cookie Vue.prototype.$axios = axios; // 把對(duì)象掛載vue中 ···
5. 在XX.vue中跨域請(qǐng)求數(shù)據(jù)
···
created: function() {
// 獲取輪播圖
this.$axios.get("http://127.0.0.1:8000/book/").then(response => {
console.log(response.data)
this.banner_list = response.data
}).catch(response => {
console.log(response)
})
// http://127.0.0.1:8000/book/ 這個(gè)就是后端django接口
···代碼
<template>
<div class="div1">
<el-carousel trigger="click" height="600px">
<el-carousel-item v-for="book in book_list" :key="book.index">
<a :href="book.link" rel="external nofollow" >
<img width="80%" :src="book.image" alt="">
</a>
</el-carousel-item>
</el-carousel>
</div>
</template>
<script>
export default {
name:"Book",
data(){
return {
book_list:[]
};
},
created: function() {
// 獲取輪播圖
this.$axios.get("http://127.0.0.1:8000/book/").then(response => {
console.log(response.data)
this.book_list = response.data
}).catch(response => {
console.log(response)
})
}
}
</script>
<style>
.div1 {
margin-top: 100px;
height: 1000px
}
img {
width: auto;
height: auto;
max-width: 100%;
max-height: 100%;
</style>到此這篇關(guān)于django+vue實(shí)現(xiàn)跨域的文章就介紹到這了,更多相關(guān)django vue跨域內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue 中 echart 在子組件中只顯示一次的問(wèn)題
vue推薦組件化開(kāi)發(fā),所以就把每個(gè)圖表封裝成子組件,然后在需要用到該圖表的父組件中直接使用。接下來(lái)給大家介紹vue 中 echart 在子組件中只顯示一次的問(wèn)題,需要的朋友參考下吧2018-08-08
前端vue框架select下拉數(shù)據(jù)量過(guò)大造成卡頓問(wèn)題解決辦法
這篇文章主要給大家介紹了關(guān)于前端vue框架select下拉數(shù)據(jù)量過(guò)大造成卡頓問(wèn)題解決辦法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用select具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
vue elementui tree 任意級(jí)別拖拽功能代碼
這篇文章主要介紹了vue elementui tree 任意級(jí)別拖拽功能代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Springboot運(yùn)用vue+echarts前后端交互實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)圖
我們做項(xiàng)目的時(shí)候,常常需要一些統(tǒng)計(jì)圖來(lái)展示我們的數(shù)據(jù),作為web開(kāi)發(fā)人員,會(huì)實(shí)現(xiàn)統(tǒng)計(jì)圖是我們必會(huì)的技能。我將帶大家來(lái)實(shí)現(xiàn)動(dòng)態(tài)餅圖的實(shí)現(xiàn),感興趣的可以了解一下2021-06-06
Vue中Object.assign清空數(shù)據(jù)報(bào)錯(cuò)的解決方案
這篇文章主要介紹了Vue中Object.assign清空數(shù)據(jù)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
vue報(bào)錯(cuò)Failed to execute 'appendChild&apos
這篇文章主要為大家介紹了vue報(bào)錯(cuò)Failed to execute 'appendChild' on 'Node'解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
vue中el-table實(shí)現(xiàn)可拖拽移動(dòng)列和動(dòng)態(tài)排序字段
最近公司需要做個(gè)項(xiàng)目,需要拖拽表格和自定義表格字段,本文主要介紹了vue中el-table實(shí)現(xiàn)可拖拽移動(dòng)列和動(dòng)態(tài)排序字段,具有一定吃參考價(jià)值,感興趣的可以了解一下2023-12-12

