vue resource發(fā)送請(qǐng)求的幾種方式
vue resource發(fā)送請(qǐng)求,代碼如下所示;
<!DOCTYPE html>
<html>
<head>
<title>vue-resource</title>
<meta charset="utf-8">
</head>
<body>
<div id="app">
<input type="button" value="get請(qǐng)求" @click="getInfo">
<input type="button" value="post請(qǐng)求" @click="postInfo">
</div>
</body>
<!-- 基于vue-resource實(shí)現(xiàn)get post請(qǐng)求 也可利用axios第三方包實(shí)現(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庫時(shí)需要注意順序,應(yīng)先引入vue
let vm = new Vue({
el: "#app",
data:{
},
methods:{//vue-resource發(fā)送請(qǐng)求是基于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è)置這個(gè)參數(shù)相當(dāng)于application/x-www-form-urlencoded,由于手動(dòng)提交請(qǐng)求沒有默認(rèn)表單格式,需要設(shè)置發(fā)送的數(shù)據(jù)格式
})
.then(res=>{
console.log(res.body);
})
},
}
});
</script>
</html>
知識(shí)點(diǎn)擴(kuò)展;vue-resource請(qǐng)求的幾種方式

安裝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ù)錯(cuò)誤",
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ā)送請(qǐng)求的幾種方式,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- 基于vue-resource jsonp跨域問題的解決方法
- Vue添加請(qǐng)求攔截器及vue-resource 攔截器使用
- vue resource post請(qǐng)求時(shí)遇到的坑
- vue插件vue-resource的使用筆記(小結(jié))
- vue-cli項(xiàng)目如何使用vue-resource獲取本地的json數(shù)據(jù)(模擬服務(wù)端返回?cái)?shù)據(jù))
- Vue2.0利用vue-resource上傳文件到七牛的實(shí)例代碼
- Vue resource中的GET與POST請(qǐng)求的實(shí)例代碼
- vue中axios處理http發(fā)送請(qǐng)求的示例(Post和get)
- VUE axios發(fā)送跨域請(qǐng)求需要注意的問題
- Vue.js實(shí)戰(zhàn)之使用Vuex + axios發(fā)送請(qǐng)求詳解
相關(guān)文章
VueJs與ReactJS和AngularJS的異同點(diǎn)
這篇文章主要為大家詳細(xì)介紹了VueJs與ReactJS和AngularJS的異同點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
vue改變對(duì)象或數(shù)組時(shí)的刷新機(jī)制的方法總結(jié)
這篇文章主要介紹了vue改變對(duì)象或數(shù)組時(shí)的刷新機(jī)制的方法總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04
vue?Element?UI擴(kuò)展內(nèi)容過長使用tooltip顯示
這篇文章主要為大家介紹了vue?Element?UI擴(kuò)展內(nèi)容過長使用tooltip展示鼠標(biāo)hover時(shí)的提示信息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Vue組件實(shí)現(xiàn)評(píng)論區(qū)功能
這篇文章主要為大家詳細(xì)介紹了Vue組件實(shí)現(xiàn)評(píng)論區(qū)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
關(guān)于Element-UI可編輯表格的實(shí)現(xiàn)過程
這篇文章主要介紹了關(guān)于Element-UI可編輯表格的實(shí)現(xiàn)過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
vue處理get/post的http請(qǐng)求的實(shí)例
本文主要介紹了vue處理get/post的http請(qǐng)求的實(shí)例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Vue+Elementui el-tree樹只能選擇子節(jié)點(diǎn)并且支持檢索功能
這篇文章給大家介紹了Vue+Element UI el-tree樹只能選擇子節(jié)點(diǎn)并且支持檢索的文章,通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-12-12
使用vue-cli(vue腳手架)快速搭建項(xiàng)目的方法
本篇文章主要介紹了使用vue-cli(vue腳手架)快速搭建項(xiàng)目的方法,vue-cli 是一個(gè)官方發(fā)布 vue.js 項(xiàng)目腳手架,使用 vue-cli 可以快速創(chuàng)建 vue 項(xiàng)目,感興趣的小伙伴們可以參考一下2018-05-05
vue項(xiàng)目中如何調(diào)用多個(gè)不同的ip接口
這篇文章主要介紹了vue項(xiàng)目中如何調(diào)用多個(gè)不同的ip接口,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

