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地址

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐