devServer配置后解决前端跨域问题
·
devServer: {
// host: '0.0.0.0',
host: 'localhost',
hot: true,
port: 3000,
open: true,
historyApiFallback: true,
https: false,
proxy: {
'/api': {
target: 'http://127.0.0.1:80', // Server
// changeOrigin: true,
pathRewrite: {'^/api': ''}
},
}
},
前端代码:
import axios from "axios";
axios.get('/api/jsonp.php?')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
<?php
// header('Content-type: application/json');
//获取回调函数名
// $callBack = htmlspecialchars($_REQUEST ['callBack']);
//json数据
$header = apache_request_headers();
foreach ($header as $headers => $value) {
echo "$headers: $value '\n'";
}
$arr = '["hello","hi"]';
//输出jsonp格式的数据
// echo $callBack . "(" . $arr . ")"+<br />';
echo $arr;
?>
后端代码:
changeOrigin可以改变服务器获取host地址


更多推荐
所有评论(0)