uni-app https无法访问,报Proxy error

uni-app https无法访问,报Proxy error

uni.request 访问服务器https接口报错: Proxy error: Could not proxy request /json/login from localhost:8080 to https://##### 但同样接口http正常 https接口本身也正常,本机用api接口调试器访问没问题 这是代码:


| 开发环境 | 版本号 | 项目创建方式 |
|---------|-------|--------------|
| 本地开发 | 未知  | 未知         |

uni.request({
url
data
method:'POST',
withCredentials:true,
sslVerify:false
}
跪求大神帮忙看看

更多关于uni-app https无法访问,报Proxy error的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

web端? 得在manifest.json中配配置webpack-dev-server代理 参考下 https://juejin.cn/post/6844904063855755271

更多关于uni-app https无法访问,报Proxy error的实战教程也可以访问 https://www.itying.com/category-93-b0.html


针对你提到的 uni-app 中 HTTPS 无法访问并报 Proxy error 的问题,这通常是由于网络请求配置不当或代理服务器设置错误引起的。以下是一些可能帮助你解决问题的代码和配置示例。

1. 检查网络请求配置

首先,确保你的 uni-app 项目中的网络请求配置正确。如果你使用的是 uni.request 方法,检查请求的 URL 是否正确,特别是协议部分(确保是 https 而不是 http)。

uni.request({
    url: 'https://your-api-endpoint.com/data', // 确保 URL 正确且为 HTTPS
    method: 'GET',
    success: (res) => {
        console.log(res.data);
    },
    fail: (err) => {
        console.error('Request failed:', err);
    }
});

2. 配置代理(如果使用了代理)

如果你在开发环境中使用了代理服务器,确保代理配置正确。在 manifest.json 中,你可以配置开发服务器代理:

{
    "mp-weixin": { // 示例平台为微信小程序,根据实际情况调整
        "devServer": {
            "proxy": {
                "/api": {
                    "target": "https://your-backend-server.com", // 代理目标地址
                    "changeOrigin": true,
                    "pathRewrite": { "^/api": "" }
                }
            }
        }
    }
}

确保 target 是 HTTPS 地址,且 changeOrigin 设置为 true 以处理跨域请求。

3. 检查证书问题

如果目标服务器使用的是自签名证书或非受信任的证书颁发机构(CA)颁发的证书,这可能导致请求失败。你可以尝试在开发环境中忽略 SSL 证书验证(注意:这仅适用于开发环境,生产环境应避免此做法)。

// 使用 axios 示例(如果你选择使用 axios 替代 uni.request)
const axios = require('axios');

axios.defaults.httpsAgent = new https.Agent({  
    rejectUnauthorized: false  // 开发环境中忽略 SSL 证书验证
});

axios.get('https://your-api-endpoint.com/data')
    .then(response => {
        console.log(response.data);
    })
    .catch(error => {
        console.error('Error:', error);
    });

注意:上述代码示例使用了 Node.js 的 https 模块,如果你直接在 uni-app 中使用,可能需要相应的环境支持或调整。

4. 调试和日志

最后,确保查看控制台和网络请求的详细日志,以获取更多关于 Proxy error 的具体信息。这有助于进一步定位问题。

如果以上步骤仍然无法解决问题,建议检查服务器端的日志和配置,或联系服务器管理员以获取帮助。

回到顶部