vue resource發(fā)送請求的幾種方式
vue resource發(fā)送請求,代碼如下所示;
<!DOCTYPE html> <html> <head> <title>vue-resource</title> <meta charset="utf-8"> </head> <body> <div id="app"> <input type="button" value="get請求" @click="getInfo"> <input type="button" value="post請求" @click="postInfo"> </div> </body> <!-- 基于vue-resource實現(xiàn)get post請求 也可利用axios第三方包實現(xiàn)--> <script src="https://cdn.staticfile.org/vue/2.6.10/vue.js"></script> <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.js"></script> <script> // vue-promise是基于vue的,所以引入CDN庫時需要注意順序,應(yīng)先引入vue let vm = new Vue({ el: "#app", data:{ }, methods:{//vue-resource發(fā)送請求是基于promise的 getInfo(){ this.$http.get('https://www.easy-mock.com/mock/5d537a1cf651bc6ff265fb77/example/result/cart.json') .then(res=>{ console.log(res); }) }, postInfo(){ this.$http.post('https://www.easy-mock.com/mock/5d537a1cf651bc6ff265fb77/example/upload',{},{ emulateJSON:true//設(shè)置這個參數(shù)相當(dāng)于application/x-www-form-urlencoded,由于手動提交請求沒有默認(rèn)表單格式,需要設(shè)置發(fā)送的數(shù)據(jù)格式 }) .then(res=>{ console.log(res.body); }) }, } }); </script> </html>
知識點擴(kuò)展;vue-resource請求的幾種方式
安裝import VueResource from 'vue-resource'
Vue.use(VueResource) get //獲取 getdesignatepl({commit,rootState}){ // console.log("ppppp") let designate_list = `schedule-plan-configuration`; Vue.http.get(api.get(designate_list),{headers:rootState.headers}).then(function(response){ let pup=response.data.data; // console.log(response) },function(response){ // console.log("pl") var tipsObj = { title : "數(shù)據(jù)錯誤", flag: true, type: 4, btnGroup:['ook'] } commit('popup',tipsObj,{root:true}); }) } post//新增 setAdd({commit,rootState,state,dispatch,context},mss){ Vue.http.post(api.save('schedule-plan-configuration'),mss,{headers:rootState.headers}).then(function(response){ dispatch("getdesignatepl") commit('popup',titleObj,{root:true}); },function(response){ if(response.status == 422){ commit('popup',tipsObj,{root:true}); } }) }, //刪除 BrDelete(context,obj){ let br_delete_put_data = `schedule-plan-configuration/${obj.id}` Vue.http.delete(api.get(br_delete_put_data),{headers:context.rootState.headers}).then(function(response){ context.dispatch("getdesignatepl"); },function(response){ // console.log(response); if(response.status == 401 || response.status == 500){ var tipsObj = { status: response.status, flag: true, type: 4, btnGroup:['ook'] } context.commit('popup',tipsObj,{root:true}); return; } }) }, // 跟新 getEntrySales({commit,rootState,dispatch,context},obj){ // console.log(obj.id) Vue.http.put(api.update(`schedule-plan-configuration/${obj.id}`),obj, {headers:rootState.headers}).then(function(response){ dispatch("getdesignatepl"); },function(response){ }) },
總結(jié)
以上所述是小編給大家介紹的vue resource發(fā)送請求的幾種方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
- 基于vue-resource jsonp跨域問題的解決方法
- Vue添加請求攔截器及vue-resource 攔截器使用
- vue resource post請求時遇到的坑
- vue插件vue-resource的使用筆記(小結(jié))
- vue-cli項目如何使用vue-resource獲取本地的json數(shù)據(jù)(模擬服務(wù)端返回數(shù)據(jù))
- Vue2.0利用vue-resource上傳文件到七牛的實例代碼
- Vue resource中的GET與POST請求的實例代碼
- vue中axios處理http發(fā)送請求的示例(Post和get)
- VUE axios發(fā)送跨域請求需要注意的問題
- Vue.js實戰(zhàn)之使用Vuex + axios發(fā)送請求詳解
相關(guān)文章
electron + vue項目實現(xiàn)打印小票功能及實現(xiàn)代碼
這篇文章主要介紹了electron + vue項目實現(xiàn)打印小票功能,需要的朋友可以參考下2018-11-11Vue+tracking.js 實現(xiàn)前端人臉檢測功能
這篇文章主要介紹了Vue+tracking.js 實現(xiàn)前端人臉檢測功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04vue中提示$index is not defined錯誤的解決方式
這篇文章主要介紹了vue中提示$index is not defined錯誤的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09element?實現(xiàn)導(dǎo)航欄收起展開功能及思路
這篇文章主要介紹了element?實現(xiàn)導(dǎo)航欄收起展開功能,實現(xiàn)思路先給 el-menu加上 :collapse="isCollapse" 屬性,這個屬性也是 element 上的一個參數(shù),意思為是否開啟折疊動畫,在 data 中定義 isCollapse ,用 true 和 false 控制展開與收起,需要的朋友可以參考下2023-01-01elementui使用el-upload組件如何實現(xiàn)自定義上傳
這篇文章主要介紹了elementui使用el-upload組件如何實現(xiàn)自定義上傳,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08vue中axios利用blob實現(xiàn)文件瀏覽器下載方式
這篇文章主要介紹了vue中axios利用blob實現(xiàn)文件瀏覽器下載方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05