1. Vue前端請求數(shù)據(jù)方法
this.$ajax.post('/api/login', this.qs.stringify(this.validateForm)).then( res => {
if (res.data.status == 0)
{
this.dialogVisible = false;
window.location.reload()
}
}).catch( res => {
})
- this.$ajax,這個是在main.js中已經(jīng)聲明了的
- 在上節(jié)Vue跨域設(shè)置中,將/api替代了http://127.0.0.1:8081/horizon
- this.qs.stringify,qs已經(jīng)在main.js中聲明了,這條語句將json格式的內(nèi)容封裝成表單格式,發(fā)送給后端,如果不這樣做,后端收不到要提交的內(nèi)容。
2. 后端django接收數(shù)據(jù)方法
@csrf_exempt
def login(request):
username = request.POST.get('username')
user_domain_name = request.POST.get('domain')
password = request.POST.get('password')
@csrf_exempt這個必須要有,否則跨域不了
- 暴露出url
編輯horizon/urls.py加入url
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('login',views.login, name='login'),
]
注意:
下一節(jié)內(nèi)容:django調(diào)用openstack api(keystone)