解決react專案中跨域和axios封裝使用

2021-10-02 06:43:01 字數 1881 閱讀 4677

1、開啟專案生成的package.json檔案,修改檔案內容如下(在此位置新增如下**):

"proxy": "http://服務位址********"
2、建立http.js,**如下:

//axios封裝

import axios from 'axios'

import from 'antd';

//響應***

axios.interceptors.response.use(

response => ,

error => }})

export function get(url, params)

s[14] = "4";

s[19] = hexdigits.substr((s[19] & 0x3) | 0x8, 1);

s[8] = s[13] = s[18] = s[23] = "-";

var num_only = s.join("");

let url2 = ''

if(url.indexof('?')!==-1)else if(params===''||params===undefined)else

axios.get(url2, )

.then(res => else

}).catch(err => )

});}export function post(url, params,token)

}).then(res => else

}).catch(err => );

});}export function post2(url, params) )

.then(res => else

}).catch(err => )

});}export function put(url, params)

}).then(res => else

}).catch(err => )

});}//匯出

export function post3(url, params) ,

responsetype:'arraybuffer',

}).then(res => )

.catch(err => )

});}

3、建立api.js,**如下:

//介面統一管理

//登入

export const userlogin = params => post('/api/user/login', params);

//退出

export const userlogout = params => post('/api/user/logout', params);

4、引用(login.js)

import  from '../../request/api.js'
handlesubmit = () =>  恭喜你,您通過本次表單元件學習,當前密碼為:$`)

this.entericonloading()}})

}

entericonloading = () => 

userlogin(params).then(response => else if (response.data.code !== 0)

}).catch(err => else

});}

拿走不謝~

vue cli專案中的跨域問題

一 開發環境中的跨域 vue cli建立的專案中,前端開發測試中,常會遇到跨域的問題。跨域通常都需要後台配置,不過前端也可以處理,可以直接利用 node.js 伺服器,通過修改 proxytable 實現跨域請求 在config資料夾下的index.js配置中 dev 設定完成一定要重啟 npm r...

跨域實際專案中處理方法

在公司開放平台的專案中,很榮幸我負責這個專案的前端工作 這個專案我們時候了前後端分離,後續出現了一些列的跨域問題 首先的問題是對介面訪問的跨域,這個問題我們討論了很久,開始擬定2種方案,第一種是使用jsonp的方式,第二種是伺服器,允許某個網域名稱訪問,最終我們採取了後者 即使是允許開放平台的網域名...

react腳手架解決跨域

配置 方法一.axios proxy 找檔案 node modules react scripts config webpackdevserver.config.js 通過正向 去獲取資料,解決跨域問題 proxy 2.改package.json scripts 3.專案根目錄下建立 config ...