您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Vue.js如何使用axios實現(xiàn)前后端數(shù)據(jù)交互”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Vue.js如何使用axios實現(xiàn)前后端數(shù)據(jù)交互”吧!
axios.get(site_url + "get_biz_list/").then(res => { if (res.data.result){ this.bkBizData = res.data.data; }else{ this.$message.error('獲取業(yè)務(wù)失敗'); } },'json');
對應(yīng)后端代碼:
def get_biz_list(request): fields = ["bk_biz_id", "bk_biz_name"] data = cc_search_business(fields) return JsonResponse({"result": True, "data": data})
axios.get(site_url + "search_host/?set_id=" + this.addSet).then(res => { if (res.data.result){ this.hostData = res.data.data; }else{ this.$message.error('獲取業(yè)務(wù)失敗'); } },'json');
對應(yīng)后端代碼
def search_host(request): set_id = request.GET.get("set_id") ... return JsonResponse({"result": ...})
axios.get(site_url + "host_view/",{params: {search_biz_id: this.searchBiz, query_str: this.searchHostIp}}).then(res => { if (res.data.result){ this.hostData = res.data.data; }else{ this.$message.error('獲取模板失敗'); } },'json');
對應(yīng)后端代碼
class HostView(CsrfExemptView): def get(self, request, *args, **kwargs): search_biz_id = request.GET.get("search_biz_id") query_str = request.GET.get("query_str") ...
axios.post(site_url + "host_view/", {"host_id": row.host_id,"is_monitored": row.is_monitored}).then(res => { if (res.data.result) { if(row.is_monitored){ this.$message.success('主機移除監(jiān)控隊列成功'); } else { this.$message.warning('主機加入監(jiān)控隊列成功'); } this.getSearch(); } else { this.$message.error('更新主機監(jiān)控狀態(tài)失敗'); } }, 'json');
對應(yīng)后端代碼
class HostView(CsrfExemptView): ... def post(self, request, *args, **kwargs): data = json.loads(request.body) host_id = data.get("host_id") is_monitored = data.get("is_monitored") ...
axios.put(site_url + "temp_view/", this.editForm).then(res => { if (res.data.result) { this.$message.success('更新模板成功'); this.editDialog = false; this.init(); } else { this.$message.error('更新模板失敗'); } }, 'json');
對應(yīng)后端代碼
class TemplateView(CsrfExemptView): ... def put(self, request, *args, **kwargs): data = json.loads(request.body) pk = data.get("pk") bk_biz_id = data.get("edit_bk_biz").split(":")[0] bk_biz_name = data.get("edit_bk_biz").split(":")[1] temp_name = data.get("edit_temp_name") script = data.get("edit_temp_script") threshold = data.get("edit_temp_value") note = data.get("edit_temp_note") temp_obj = { "bk_biz_id": bk_biz_id, "bk_biz_name": bk_biz_name, "temp_name": temp_name, "script": script, "threshold": threshold, "note": note, } try: Template.objects.filter(pk=pk).update(**temp_obj) return JsonResponse({"result": True}) except Exception as e: print(e) return JsonResponse({"result": False})
axios.delete('/temp_view/',{data:{id:row.pk}}).then(res => { if (res.data.result) { this.$message.success('刪除模板成功'); this.init(); } else { this.$message.error('刪除模板失敗'); } }, 'json');
對應(yīng)后端代碼
class TemplateView(CsrfExemptView): ... def delete(self, request, *args, **kwargs): data = json.loads(request.body) pk = data.get("id") try: Template.objects.filter(pk=pk).delete() return JsonResponse({"result": True}) except Exception: return JsonResponse({"result": False})
到此,相信大家對“Vue.js如何使用axios實現(xiàn)前后端數(shù)據(jù)交互”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。