vue?axios中的get請求方式
更新時間:2022年07月02日 10:37:57 作者:山竹回家了
這篇文章主要介紹了vue?axios中的get請求方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
vue axios中的get請求
一、安裝
使用 npm:npm install axios
二、使用
步驟:1.引入 2.發(fā)送請求
<template> ? <div> ? ? <!-- 2.點擊發(fā)送請求 --> ? ? <button @click="getdata">get請求-無參數(shù)</button ? ? ><button @click="getDataByParams">get請求-有參數(shù)</button> ? </div> </template>
<script> //1.引入axios import axios from "axios"; export default { ? methods: { ? ? // 3.發(fā)送axios無參數(shù)請求 ? ? getdata() { ? ? ? axios ? ? ? ? // 3.1url地址 ? ? ? ? .get("http://157.122.54.189:9095/scenics/banners") ? ? ? ? // 3.2成功時回調(diào)函數(shù) ? ? ? ? .then((data) => { ? ? ? ? ? console.log(data); ? ? ? ? }) ? ? ? ? //3.3失敗時回調(diào)函數(shù) ? ? ? ? .catch((err) => { ? ? ? ? ? console.log(err); ? ? ? ? }); ? ? }, ? ? // 有參數(shù) ? ? getDataByParams() { ? ? ? axios ? ? ? ? //params:可傳遞多個參數(shù),固定寫法,不能改,否則參數(shù)傳遞失敗 ? ? ? ? .get("http://157.122.54.189:9095/posts", { params: { id: 4 } }) ? ? ? ? .then((data) => { ? ? ? ? ? console.log(data); ? ? ? ? }) ? ? ? ? .catch((err) => { ? ? ? ? ? console.log(err); ? ? ? ? }); ? ? }, ? }, }; </script>
<style> </style>
常見錯誤
url后面不要寫冒號,否則會結(jié)束。
vue axios post請求參數(shù)錯誤400
如果直接把loginMode1當請求參數(shù)傳,后端是接收不到的
要對loginMode1處理成字符串然后再轉(zhuǎn)換,如下
然后再axios處理
這樣就不會有問題了
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3父子通信ref,toRef,toRefs使用實例詳解
這篇文章主要介紹了vue3父子通信ref,toRef,toRefs使用實例詳解,分別介紹了ref是什么、toRef是什么及toRefs是什么和最佳使用方式,結(jié)合示例代碼給大家講解的非常詳細,需要的朋友可以參考下2023-10-10vue雙擊事件2.0事件監(jiān)聽(點擊-雙擊-鼠標事件)和事件修飾符操作
這篇文章主要介紹了vue雙擊事件2.0事件監(jiān)聽(點擊-雙擊-鼠標事件)和事件修飾符操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue父組件給子組件的組件傳值provide inject的方法
在本篇文章里小編給大家整理的是一篇關(guān)于vue父組件給子組件的組件傳值provide inject的方法,需要的朋友們學(xué)習(xí)下。2019-10-10