vue 請(qǐng)求后臺(tái)數(shù)據(jù)的實(shí)例代碼
需要引用vue-resource
安裝請(qǐng)參考https://github.com/pagekit/vue-resource官方文檔
在入口函數(shù)中加入
import VueResource from 'vue-resource' Vue.use(VueResource);
在package.json文件中加入
"dependencies": {
"vue": "^2.2.6",
"vue-resource":"^1.2.1"
},
請(qǐng)求如下
mounted: function () {
// GET /someUrl
this.$http.get('http://localhost:8088/test').then(response => {
console.log(response.data);
// get body data
// this.someData = response.body;
}, response => {
console.log("error");
});
},
注意
1.在請(qǐng)求接口數(shù)據(jù)時(shí),涉及到跨域請(qǐng)求
出現(xiàn)下面錯(cuò)誤:
XMLHttpRequest cannot load http://localhost:8088/test. No ‘Access-Control-Allow-Origin' header is present on the requested resource. Origin ‘http://localhost:8080' is therefore not allowed access.
解決辦法:在接口中設(shè)置
response.setHeader("Access-Control-Allow-Origin", "*");
2.使用jsonp請(qǐng)求
但是出現(xiàn)如下錯(cuò)誤
Uncaught SyntaxError: Unexpected token
查看請(qǐng)求,數(shù)據(jù)已返回,未解決.
提交表單
<div id="app-7">
<form @submit.prevent="submit">
<div class="field">
姓名:
<input type="text"
v-model="user.username">
</div>
<div class="field">
密碼:
<input type="text"
v-model="user.password">
</div>
<input type="submit"
value="提交">
</form>
</div>
methods: {
submit: function() {
var formData = JSON.stringify(this.user); // 這里才是你的表單數(shù)據(jù)
this.$http.post('http://localhost:8088/post', formData).then((response) => {
// success callback
console.log(response.data);
}, (response) => {
console.log("error");
// error callback
});
}
},
提交restful接口出現(xiàn)跨域請(qǐng)求的問題
查閱資料得知,
當(dāng)contentType設(shè)置為三個(gè)常用的格式以外的格式,如“application/json”時(shí),會(huì)先發(fā)送一個(gè)試探的OPTIONS類型的請(qǐng)求給服務(wù)端。在這時(shí),單純的在業(yè)務(wù)接口response添加Access-Control-Allow-Origin 由于還沒有走到所以不會(huì)起作用。
解決方案:
在服務(wù)端增加一個(gè)攔截器
用于處理所有請(qǐng)求并加上允許跨域的頭
public class CommonInterceptor implements HandlerInterceptor {
private List<String> excludedUrls;
public List<String> getExcludedUrls() {
return excludedUrls;
}
public void setExcludedUrls(List<String> excludedUrls) {
this.excludedUrls = excludedUrls;
}
/**
*
* 在業(yè)務(wù)處理器處理請(qǐng)求之前被調(diào)用 如果返回false
* 從當(dāng)前的攔截器往回執(zhí)行所有攔截器的afterCompletion(),
* 再退出攔截器鏈, 如果返回true 執(zhí)行下一個(gè)攔截器,
* 直到所有的攔截器都執(zhí)行完畢 再執(zhí)行被攔截的Controller
* 然后進(jìn)入攔截器鏈,
* 從最后一個(gè)攔截器往回執(zhí)行所有的postHandle()
* 接著再從最后一個(gè)攔截器往回執(zhí)行所有的afterCompletion()
*
* @param request
*
* @param response
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "*");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept");
return true;
}
// 在業(yè)務(wù)處理器處理請(qǐng)求執(zhí)行完成后,生成視圖之前執(zhí)行的動(dòng)作
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
/**
*
* 在DispatcherServlet完全處理完請(qǐng)求后被調(diào)用
* 當(dāng)有攔截器拋出異常時(shí),
* 會(huì)從當(dāng)前攔截器往回執(zhí)行所有的攔截器的afterCompletion()
*
* @param request
*
* @param response
*
* @param handler
*
*/
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
}
}
spring resultful無法像在jsp提交表單一樣處理數(shù)據(jù)必須加上@RequestBody,可以直接json轉(zhuǎn)換object,但是對(duì)與沒有bean的表單數(shù)據(jù),建議轉(zhuǎn)換為map對(duì)象,類似@RequestBody Map、
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue如何從接口請(qǐng)求數(shù)據(jù)
- vue.js實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)的方法示例
- vuejs前后端數(shù)據(jù)交互之從后端請(qǐng)求數(shù)據(jù)的實(shí)例
- Vue2學(xué)習(xí)筆記之請(qǐng)求數(shù)據(jù)交互vue-resource
- vue請(qǐng)求數(shù)據(jù)的三種方式
- vue中promise的使用及異步請(qǐng)求數(shù)據(jù)的方法
- vue中實(shí)現(xiàn)先請(qǐng)求數(shù)據(jù)再渲染dom分享
- 談一談vue請(qǐng)求數(shù)據(jù)放在created好還是mounted里好
- vue2實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求顯示loading圖
- Vue.js+HighCharts實(shí)現(xiàn)動(dòng)態(tài)請(qǐng)求展示時(shí)序數(shù)據(jù)
相關(guān)文章
Vue+Echarts實(shí)現(xiàn)分時(shí)圖和交易量圖的繪制
近來發(fā)現(xiàn)Echarts?API越發(fā)的強(qiáng)大,對(duì)于繪制各類圖形可以使用Echarts實(shí)現(xiàn)。本文將利用Echarts實(shí)現(xiàn)分時(shí)圖和交易量圖的繪制,希望對(duì)大家有所幫助2023-03-03
分享一個(gè)精簡(jiǎn)的vue.js 圖片lazyload插件實(shí)例
本篇文章主要介紹了分享一個(gè)精簡(jiǎn)的vue.js 圖片lazyload插件實(shí)例。非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03
Vue + element實(shí)現(xiàn)動(dòng)態(tài)顯示后臺(tái)數(shù)據(jù)到options的操作方法
最近遇到一個(gè)需求需要實(shí)現(xiàn)selector選擇器中選項(xiàng)值options 數(shù)據(jù)的動(dòng)態(tài)顯示,而非寫死的數(shù)據(jù),本文通過實(shí)例代碼給大家分享實(shí)現(xiàn)方法,感興趣的朋友一起看看吧2021-07-07
vue router下的html5 history在iis服務(wù)器上的設(shè)置方法
這篇文章主要介紹了vue router下的html5 history在iis服務(wù)器上的設(shè)置方法,需要的朋友參考下吧2017-10-10
vue 使用 v-model 雙向綁定父子組件的值遇見的問題及解決方案
這篇文章主要介紹了vue 使用 v-model 雙向綁定父子組件的值遇見的問題及解決方案,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-03-03
手寫Vue源碼之?dāng)?shù)據(jù)劫持示例詳解
這篇文章主要給大家介紹了手寫Vue源碼之?dāng)?shù)據(jù)劫持的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

