axios 代理设置跨域出现错误

**请求.png/.html资源是可以的
但是请求.php就出现了问题**

之前我用Wampserver32跑php
当时的代理是

'/sign': {          
        target: 'http://localhost:8088',
        changeOrigin: true,
        ws: true,
        pathRewrite: {
            '^/sign': '/'
        }
      },

是没问题的 现在我直接通过终端运行

$php -S localhost:8088

test.php文件目录是在项目里面的api文件夹下

这个是终端提示

Proxy error: Could not proxy request //test.php from 192.168.1.12:1010 to http://locahost:8088.
See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ENOTFOUND).

clipboard.png

代理设置
clipboard.png

api.js
clipboard.png

浏览器控制台
clipboard.png

test.php

<?php
  header('Content-Type: application/json');
  header('Access-Control-Allow-Origin: *');
  header('Access-Control-Allow-Method: POST,GET');  
  header('Access-Control-Allow-Headers:Content-Type, api_key, Authorization');
  echo 'hello world';

?>

test.php运行($php -S localhost:8088)结果
clipboard.png

阅读 6.9k
3 个回答
✓ 已被采纳新手上路,请多包涵

问题解决了...
问题不是跨域..T T,
是php设置返回格式错误了
同时加了

npm install http-parser-js    

在 vue.config.js

process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;

浏览器直接访问是 get请求,你代码是用的 post 请求,改成 get 试试。

图片描述

把这里修改下:

    
    pathRewrite:{
        '^/test': ''
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题