【发布时间】:2020-10-01 13:23:43
【问题描述】:
我正在尝试通过 POST 从 create-react-app 应用程序调用 Web 服务到在 XAMPP 上运行的本地虚拟主机(也尝试过 WAMP),并以 Windows 10 作为操作系统。
本地环境
-
http://test.local/- 在 XAMPP 虚拟主机上运行的本地服务器。 -
http://localhost:3000- 创建反应应用程序网址
什么调用在每种情况下都有效
-
axios.get正确响应和axios.post正确响应来自test.local/。 -
axios.get正确响应和axios.post不响应。当由localhost:3000制成时
我的测试中使用的代码
axios.post 来自localhost:3000 的请求失败
axios.post('http://test.local/load/notifications/', { someData: 'someVal'},
{ headers: { "Access-Control-Allow-Origin": "*" }).then((resp) => {
console.log(resp.data); // no response in network tab of developer tools
}, (error) => {
// The request was made but no response was received
})
我已经尝试过这个请求有和没有配置对象中的CORS设置。
PHP 测试文件位于test.local/load/notifications/index.php
<?php
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
$mockDataFile = "./mockData.json";
$unencoded = json_decode(file_get_contents($mockDataFile));
echo json_encode($unencoded);
?>
我在 php 页面上的 CORS 标头设置为
header("Access-Control-Allow-Origin: *");,所以我假设不是 除非我的语法关闭,否则 CORS 相关。没有错误信息, 端点只是不返回响应。
httpd.conf 中的目录标志
<Directory />
AllowOverride none
Require local
Require ip 192.168.1.5
</Directory>
httpd-vhosts.conf中的vhost配置:
<VirtualHost *:80>
DocumentRoot "F:\test\public"
<Directory "F:\test\public">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
</Directory>
ServerName test.local
ServerAlias test.local
CustomLog "logs/test.local-access.log" common
ErrorLog "logs/test.local-error.log"
</VirtualHost>
【问题讨论】:
-
我原来的问题是误导的原因。我将保留原来的问题,以防其他人发现问题,就像我花了几个小时在谷歌上搜索问题而没有结果一样。这是一个毁了我一天的晦涩难懂的问题,所以希望这能减轻一些人的痛苦。
-
如果您选择投反对票,请发表评论,以便我改进我的条目
标签: apache axios cors wamp vhosts