vue 請求后臺數(shù)據(jù)的實例代碼
需要引用vue-resource
安裝請參考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"
},
請求如下
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.在請求接口數(shù)據(jù)時,涉及到跨域請求
出現(xiàn)下面錯誤:
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請求
但是出現(xiàn)如下錯誤
Uncaught SyntaxError: Unexpected token
查看請求,數(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)跨域請求的問題
查閱資料得知,
當(dāng)contentType設(shè)置為三個常用的格式以外的格式,如“application/json”時,會先發(fā)送一個試探的OPTIONS類型的請求給服務(wù)端。在這時,單純的在業(yè)務(wù)接口response添加Access-Control-Allow-Origin 由于還沒有走到所以不會起作用。
解決方案:
在服務(wù)端增加一個攔截器
用于處理所有請求并加上允許跨域的頭
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ù)處理器處理請求之前被調(diào)用 如果返回false
* 從當(dāng)前的攔截器往回執(zhí)行所有攔截器的afterCompletion(),
* 再退出攔截器鏈, 如果返回true 執(zhí)行下一個攔截器,
* 直到所有的攔截器都執(zhí)行完畢 再執(zhí)行被攔截的Controller
* 然后進入攔截器鏈,
* 從最后一個攔截器往回執(zhí)行所有的postHandle()
* 接著再從最后一個攔截器往回執(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ù)處理器處理請求執(zhí)行完成后,生成視圖之前執(zhí)行的動作
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
/**
*
* 在DispatcherServlet完全處理完請求后被調(diào)用
* 當(dāng)有攔截器拋出異常時,
* 會從當(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,但是對與沒有bean的表單數(shù)據(jù),建議轉(zhuǎn)換為map對象,類似@RequestBody Map、
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue如何從接口請求數(shù)據(jù)
- vue.js實現(xiàn)請求數(shù)據(jù)的方法示例
- vuejs前后端數(shù)據(jù)交互之從后端請求數(shù)據(jù)的實例
- Vue2學(xué)習(xí)筆記之請求數(shù)據(jù)交互vue-resource
- vue請求數(shù)據(jù)的三種方式
- vue中promise的使用及異步請求數(shù)據(jù)的方法
- vue中實現(xiàn)先請求數(shù)據(jù)再渲染dom分享
- 談一談vue請求數(shù)據(jù)放在created好還是mounted里好
- vue2實現(xiàn)數(shù)據(jù)請求顯示loading圖
- Vue.js+HighCharts實現(xiàn)動態(tài)請求展示時序數(shù)據(jù)
相關(guān)文章
Vue+Echarts實現(xiàn)分時圖和交易量圖的繪制
近來發(fā)現(xiàn)Echarts?API越發(fā)的強大,對于繪制各類圖形可以使用Echarts實現(xiàn)。本文將利用Echarts實現(xiàn)分時圖和交易量圖的繪制,希望對大家有所幫助2023-03-03
Vue + element實現(xiàn)動態(tài)顯示后臺數(shù)據(jù)到options的操作方法
最近遇到一個需求需要實現(xiàn)selector選擇器中選項值options 數(shù)據(jù)的動態(tài)顯示,而非寫死的數(shù)據(jù),本文通過實例代碼給大家分享實現(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ì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

