简体   繁体   English

如何在 Vue3 + Vite 开发服务器中运行 php 脚本

[英]How to run php scripts within Vue3 + Vite dev server

I am making a small project in Vue3 using the recommended dev server setup http://localhost:5173.我正在使用推荐的开发服务器设置 http://localhost:5173 在 Vue3 中制作一个小项目。 The files are all on a linux apache web server in /var/www/html/etc.. but php scripts won't run from within localhost:5173.这些文件都在 linux apache web 服务器上 /var/www/html/etc.. 但 php 脚本不会从 localhost:5173 中运行。

When I try to access the php files directly:当我尝试直接访问 php 文件时:

await fetch('http://localhost/vue-project/src/php/test.php', {method: 'GET'})

I get the following error:我收到以下错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource跨源请求被阻止:同源策略不允许读取远程资源

Can anyone help me with a solution to this please?谁能帮我解决这个问题?

I found a solution here: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at url我在这里找到了一个解决方案: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at url

..is to access the php script using the full url (http://localhost/etc...) and to put the following lines of code at the top of any php script: ..是使用完整的 url (http://localhost/etc...) 访问 php 脚本,并将以下代码行放在任何 php 脚本的顶部:

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

Because this is just for the purposes of getting the php to work whilst 'npm run dev', after 'npm run build' you could then remove those lines of code and just change the javascript to reference the php files directly.因为这只是为了让 php 在“npm run dev”时工作,所以在“npm run build”之后,您可以删除这些代码行,只需更改 javascript 以直接引用 php 文件。

Any other recommendations would be welcome.欢迎任何其他建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM