【发布时间】:2018-09-09 22:17:25
【问题描述】:
我在设置我的 create-react-app 应用程序以将请求代理到我在 Microsoft azure 上的测试主机时遇到了一些问题。我在我的应用程序的 package.json 中设置了代理,如下所示:
"proxy":{
"/api/*":{
"target":"https://mytestbackend.azurewebsites.net",
"secure":false
}
}
我已经设置了一个 axios 请求以发送到 azure 上的后端服务器。它位于一个独立的 .js 中,我从我的一个反应应用程序的事件中调用它。它看起来像这样:
import axios from 'axios';
const login = async (username, password) => {
console.log("Username to send is:"+username);
console.log("password to send is:"+password);
let response = await axios.post('/api/user/login', {username:username,password:password});
console.log(response);
};
export {login};
问题不可能出在我的反应组件中,因为这两个 console.log() 调用表明正在接收输入的值。如果我从 package.json 中删除 "secure":false 设置,请求会失败并显示 Http 错误:500。但如果我使用安全设置,则会失败并显示 404 页面。有人可以解释一下我做错了什么吗?我只能在“localhost”上使用代理吗?文档另有说明。非常感谢任何帮助。
我已验证在 Azure 管理门户上运行开发服务器的域启用了 CORS。如果我直接使用后端的 URL(即不使用 create-react-app 代理)来执行请求,它就可以工作。问题一定出在代理的配置方式上。
不使用安全时发生的 HTTP Errpr 500 的响应文本是:
Proxy error: Could not proxy request /api/user/login from localhost:3000 to https://mytestbackend.azurewebsites.net (undefined).
附加信息:我还通过在我的开发机器上本地运行我的后端进行了测试。出现错误消息,但括号中的“未定义”显示“UNABLE_TO_VERIFY_LEAF_SIGNATURE”。如果使用"secure: false,我可以成功调用登录端点,但是调用其他需要认证的端点会失败,因为cookie不是由axios发送的。
做: curl -v https://mytestbackend.azurewebsites.net/api/user/login
有这个输出:
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS alert, Server hello (2):
* SSL certificate problem: unable to get local issuer certificate
* Closing connection #0
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
【问题讨论】:
-
FE也是部署在同一个服务器还是不同的服务器上?如果两台服务器不同,那么您是否打开了端口和IP进行通信?另外请让 500 错误发生并发布那里的异常详细信息
-
FE 在本地主机上运行(这是我的开发环境),但连接到托管在 azure 中的后端。我正在使用 create-react-app 的代理配置来转发请求。如果我使用硬编码的 URL 向后端发出前端请求,它就可以工作。我看到的问题是,当我执行 Ajax 请求时,Axios 没有发送我的 cookie。在后端和 Azure 门户上都启用了 CORS。我会尽快发布错误 500 详细信息。
-
我已更新我的问题以包含异常的详细信息。
-
curl -v https://mytestbackend.azurewebsites.net/api/user/login的输出是什么 -
我在问题中包含了 curl 的输出。
标签: reactjs azure create-react-app