您好,登錄后才能下訂單哦!
本篇內容主要講解“Vue.js如何使用axios實現前后端數據交互”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Vue.js如何使用axios實現前后端數據交互”吧!
axios.get(site_url + "get_biz_list/").then(res => { if (res.data.result){ this.bkBizData = res.data.data; }else{ this.$message.error('獲取業務失敗'); } },'json');
對應后端代碼:
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('獲取業務失敗'); } },'json');
對應后端代碼
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');
對應后端代碼
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('主機移除監控隊列成功'); } else { this.$message.warning('主機加入監控隊列成功'); } this.getSearch(); } else { this.$message.error('更新主機監控狀態失敗'); } }, 'json');
對應后端代碼
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');
對應后端代碼
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');
對應后端代碼
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實現前后端數據交互”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。