鍍金池/ 問答/HTML/ localhost用Axios請求內(nèi)網(wǎng)數(shù)據(jù)跨域問題

localhost用Axios請求內(nèi)網(wǎng)數(shù)據(jù)跨域問題

本地用vue已經(jīng)建好了,現(xiàn)在需要讀數(shù)據(jù),公司后臺給了一個接口文檔,沒怎么看懂。
我現(xiàn)在本機是localhost,有公司服務器內(nèi)網(wǎng)地址,請求時出了2個錯:
1.OPTIONS http://xxx.xxx.xxx.xxx:9005/api/FlightInfo/FlightInfoSearch 405 (Method Not Allowed)
2.Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 405.
我的axios配置有baseURL、timeout和headers'Content-Type' : 'application/x-www-form-urlencoded'
搜了資料,解決方法一是:“后臺設置:'Access-Control-Allow-Origin':'*'”;
方法二是:

import axios from 'axios';
import qs from 'qs';

axios.post('http://www.xyz.com/request', qs.stringify(params))
.then(response => {
  console.log(response);
})
.catch(err => {
  console.log(err);
});

但是我確實不知道params是什么,我現(xiàn)在就想點一個按鈕出來所有的數(shù)據(jù),請大神指導!

回答
編輯回答
刮刮樂

用的java話可以用jsonp的方式請求具體請看鏈接 鏈接描述

如果你還用nodeJS的話 就用node代理去直接請求后臺,這樣不涉及跨域的問題,就不用更改java代碼了

2018年7月14日 01:39
編輯回答
浪婳

跨域了,服務端配置下cors就好了

2018年5月4日 15:08