跨域报错

笔记记录之跨域报错:http://localhost/访问服务器接口
跨域报错:Access to XMLHttpRequest at ‘https://www.zykjedu.cn:9443/api.html?method=store.getstorelist’ from origin ‘http://127.0.0.1’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: The value of the ‘Access-Control-Allow-Origin’ header in the response must not be the wildcard ‘*’ when the request’s credentials mode is ‘include’. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

跨域报错原因: 浏览器(Webkit内核)的安全策略决定了file协议访问的应用无法使用XMLHttpRequest对象,请求返回200但是并没有返回数据
在这里插入图片描述

解决办法:找到谷歌浏览器的安装地址,
cmd 进入终端

C:\Users\pp>cd appdata

C:\Users\pp\AppData>cd local/google

C:\Users\pp\AppData\Local\Google>cd chrome

C:\Users\pp\AppData\Local\Google\Chrome>cd Application

C:\Users\pp\AppData\Local\Google\Chrome\Application>chrome.exe --disable-web-security --user-agent="Android" --user-data-dir="C:/temp-chrome-eng

C:\Users\pp\AppData\Local\Google\Chrome\Application>

然后打开 http://127.0.0.1/home/index/index.html

记录遇到的问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp中使用uni.uploadFile进行文件上传时,如果涉及跨域请求,可能会出现跨域报错。解决跨域问题的方法有以下几种: 1. 在服务器端设置允许跨域访问:在服务器端的响应头中添加Access-Control-Allow-Origin字段,允许指定的域名或通配符(*)进行跨域访问。 例如,在Node.js的Express框架中,可以使用cors库来设置允许跨域访问: ```javascript const express = require('express'); const cors = require('cors'); const app = express(); app.use(cors()); // 处理文件上传的路由 app.post('/upload', (req, res) => { // 处理文件上传的逻辑 }); // 启动服务器 app.listen(3000, () => { console.log('服务器已启动'); }); ``` 2. 使用代理服务器:在uniapp的`config.js`配置文件中,可以配置一个代理服务器,将跨域请求转发到服务器端,从而避免跨域问题。 在`config.js`文件中添加以下代码: ```javascript module.exports = { devServer: { proxy: { '/api': { target: 'http://example.com', // 实际请求的接口地址 changeOrigin: true, pathRewrite: { '^/api': '' // 如果请求的接口路径有前缀,需要将其替换为空字符串 } } } } } ``` 然后,在uniapp中使用`uni.uploadFile`时,将请求的地址设置为`/api/upload`,例如: ```javascript uni.uploadFile({ url: '/api/upload', // 其他参数... }); ``` 这样,uniapp会将`/api/upload`的请求转发到`http://example.com/upload`。 3. 使用JSONP请求:如果服务器端支持JSONP跨域请求,可以将文件上传接口改为支持JSONP,并在uniapp中使用uni.request进行请求。 在服务器端将文件上传接口改为支持JSONP: ```javascript app.get('/upload', (req, res) => { const callback = req.query.callback; const result = { code: 0, message: '上传成功' }; const jsonpResult = `${callback}(${JSON.stringify(result)})`; res.send(jsonpResult); }); ``` 然后,在uniapp中使用uni.request进行请求: ```javascript uni.request({ url: 'http://example.com/upload', method: 'GET', dataType: 'jsonp', success(res) { console.log('文件上传成功', res.data); }, fail(err) { console.error('文件上传失败', err); } }); ``` 以上是解决Uniapp中使用uni.uploadFile跨域报错的几种方法,根据你的实际情况选择合适的方法进行处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值