1. 程式人生 > >Java的新專案學成線上筆記-day2(四)

Java的新專案學成線上筆記-day2(四)

3.3 跨域問題解決 
測試 上邊的代理 ,結果 報錯如下 :
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:11000' is therefore not allowed access.
原因:瀏覽器的同源策略不允許跨域訪問,所謂同源策略是指協議、域名、埠相同。
解決:採用proxyTable解決。 proxyTable是什麼?
vue-cli提供的解決vue開發環境下跨域問題的方法,proxyTable的底層使用了http-proxymiddleware(

https://github.com/chimurai/http-proxy-middleware),它是http代理中介軟體,它依賴node.js, 基本原理是用服務端代理解決瀏覽器跨域:
Java的新專案學成線上筆記-day2(四)
cms跨域解決原理: 1、訪問頁面http://localhost:11000/
2、頁面請求http://localhost:11000/cms

由於url由http://localhost:31001/cms...改為“http://localhost:11000/cms.",所以不存在跨域 3、通過proxyTable由node伺服器代理請求 http://localhost:31001/cms.
服務端不存在跨域問題
具體的配置如下:
1)修改api方法中url的定義 請求前加/api字首

//public是對axios的工具類封裝,定義了http請求方法 
import http from './../../../base/api/public'
 let sysConfig = require('@/../config/sysConfig')
 let apiUrl = sysConfig.xcApiUrlPre; 
export const page_list = (page,size,params) => { 
 return http.requestQuickGet(apiUrl+'/cms/page/list/'+page+'/'+size)
 }

2)在config/index.js下配置proxyTable。 以/api/cms開頭的請求,代理請求http://localhost:31001

'/api/cms': {    
   target: 'http://localhost:31001',   
    pathRewrite: {       
  '^/api': ''//實際請求去掉/api      
  }

3.4 分頁查詢測試 
1、定義分頁檢視
使用v-on監聽更改分頁事件

<el‐pagination  
     layout="prev, pager, next"
      :page‐size="this.params.size"    
  v‐on:current‐change="changePage"   
   :total="total" :current‐page="this.params.page" style="float:right;">
    </el‐pagination>

2、定義資料模型物件

data() {
       return {   
     list:[],   
     total:50,  
      params:{  
        page:1,//頁碼     
      size:2//每頁顯示個數    
     }    
  }

3、定義分頁方法,接收頁碼引數

//分頁查詢,
接收page頁碼 changePage(page){
  this.params.page = page; 
  this.query()
 }

修改完畢,測試分頁效果。